From e1afbfbbdcdaf7007e3d0acad1d4d41742086507 Mon Sep 17 00:00:00 2001 From: Kilian Saffran Date: Fri, 11 Nov 2022 16:29:37 +0100 Subject: [PATCH] v20221111 --- .vscode/settings.json | 1 + .vscode/sftp.json | 2 +- .../data/schemata/defaultcompany.schema.sql | 92 +- app_plandutravail_lu/db.php | 4 +- app_plandutravail_lu/lib/POT/Company.php | 2 +- app_plandutravail_lu/lib/POT/Period.php | 13 +- app_plandutravail_lu/lib/POT/Staff.php | 5 +- app_plandutravail_lu/lib/POT/TimeTracker.php | 4 +- app_plandutravail_lu/lib/POT/Workplan.php | 26 +- app_plandutravail_lu/lib/version.php | 2 +- app_plandutravail_lu/log/sql.log | 32376 ++++++++++++++++ app_plandutravail_lu/log/sqlx.log | 276 + app_plandutravail_lu/pwa/manifest.webmanifest | 6 +- app_plandutravail_lu/pwa/serviceworker.js | 8 +- .../tmpl/block/common_scripts.html | 2 +- .../tmpl/block/dlgaddperiod.html | 4 +- .../tmpl/module/admin/admin.html | 4 +- .../tmpl/module/admin/companies.js | 149 +- .../tmpl/module/admin/dlg_dataset.html | 36 - .../tmpl/module/admin/dlg_period.html | 53 + .../tmpl/module/admin/dlg_worktimes.html | 4 +- .../tmpl/module/admin/pnl_basedata.html | 134 +- .../tmpl/module/admin/schemadataset.js | 86 +- .../tmpl/module/admin/sectors.js | 8 +- .../tmpl/module/admin/vacancydays.js | 2 +- .../tmpl/module/js/request.js | 7 +- .../tmpl/module/periods/periodday.js | 132 +- .../tmpl/module/periods/staffperiodweeks.js | 6 +- .../tmpl/module/staff/staff.js | 11 +- .../tmpl/module/staff/staffvacancy.js | 8 +- .../tmpl/module/timeclock/timeclock.js | 7 +- .../module/workplans/dlg_workplanday.html | 92 +- .../tmpl/module/workplans/workplans.js | 186 +- dev/conf.sh | 2 +- dev/db/potlu2_db.alicehartmann.pg.schema.sql | 117 +- .../potlu2_db.newcompanyschema.pg.schema.sql | 92 +- dev/diff_app.sh | 9 +- dev/gendefaultschema.sh | 2 +- hourtrax/install/bin/NFCRead.jar | Bin 0 -> 3786 bytes hourtrax/install/bin/nfcread | 3 + .../NFCRead$ByteCastException.class | Bin 0 -> 326 bytes .../NFCRead$InstructionFailedException.class | Bin 0 -> 344 bytes .../NFCRead$TerminalNotFoundException.class | Bin 0 -> 342 bytes hourtrax/src/lu/plandutravail/NFCRead.class | Bin 0 -> 3641 bytes hourtrax/src/lu/plandutravail/NFCRead.java | 82 + timeclock_plandutravail_lu/.htaccess | 5 + timeclock_plandutravail_lu/index.php | 101 +- timeclock_plandutravail_lu/js/lang/de.js | 31 - timeclock_plandutravail_lu/js/lang/fr.js | 31 - timeclock_plandutravail_lu/lib/composer.json | 5 + timeclock_plandutravail_lu/lib/composer.lock | 252 + timeclock_plandutravail_lu/lib/config.php | 4 +- .../lib/vendor/autoload.php | 7 + .../lib/vendor/composer/ClassLoader.php | 481 + .../lib/vendor/composer/InstalledVersions.php | 337 + .../lib/vendor/composer/LICENSE | 21 + .../lib/vendor/composer/autoload_classmap.php | 10 + .../lib/vendor/composer/autoload_files.php | 11 + .../vendor/composer/autoload_namespaces.php | 9 + .../lib/vendor/composer/autoload_psr4.php | 12 + .../lib/vendor/composer/autoload_real.php | 75 + .../lib/vendor/composer/autoload_static.php | 54 + .../lib/vendor/composer/installed.json | 248 + .../lib/vendor/composer/installed.php | 50 + .../lib/vendor/composer/platform_check.php | 26 + .../vendor/symfony/polyfill-ctype/Ctype.php | 232 + .../lib/vendor/symfony/polyfill-ctype/LICENSE | 19 + .../vendor/symfony/polyfill-ctype/README.md | 12 + .../symfony/polyfill-ctype/bootstrap.php | 50 + .../symfony/polyfill-ctype/bootstrap80.php | 46 + .../symfony/polyfill-ctype/composer.json | 41 + .../vendor/symfony/polyfill-mbstring/LICENSE | 19 + .../symfony/polyfill-mbstring/Mbstring.php | 873 + .../symfony/polyfill-mbstring/README.md | 13 + .../Resources/unidata/lowerCase.php | 1397 + .../Resources/unidata/titleCaseRegexp.php | 5 + .../Resources/unidata/upperCase.php | 1489 + .../symfony/polyfill-mbstring/bootstrap.php | 147 + .../symfony/polyfill-mbstring/bootstrap80.php | 143 + .../symfony/polyfill-mbstring/composer.json | 41 + .../lib/vendor/twig/twig/.editorconfig | 18 + .../lib/vendor/twig/twig/.gitattributes | 3 + .../lib/vendor/twig/twig/.gitignore | 4 + .../lib/vendor/twig/twig/.php_cs.dist | 20 + .../lib/vendor/twig/twig/.travis.yml | 43 + .../lib/vendor/twig/twig/CHANGELOG | 1453 + .../lib/vendor/twig/twig/LICENSE | 31 + .../lib/vendor/twig/twig/README.rst | 24 + .../lib/vendor/twig/twig/composer.json | 51 + .../lib/vendor/twig/twig/doc/advanced.rst | 909 + .../vendor/twig/twig/doc/advanced_legacy.rst | 882 + .../lib/vendor/twig/twig/doc/api.rst | 588 + .../vendor/twig/twig/doc/coding_standards.rst | 101 + .../lib/vendor/twig/twig/doc/deprecated.rst | 6 + .../lib/vendor/twig/twig/doc/filters/abs.rst | 18 + .../vendor/twig/twig/doc/filters/batch.rst | 49 + .../twig/twig/doc/filters/capitalize.rst | 11 + .../vendor/twig/twig/doc/filters/column.rst | 24 + .../twig/doc/filters/convert_encoding.rst | 22 + .../twig/twig/doc/filters/country_name.rst | 42 + .../twig/twig/doc/filters/currency_name.rst | 45 + .../twig/twig/doc/filters/currency_symbol.rst | 45 + .../vendor/twig/twig/doc/filters/data_uri.rst | 51 + .../lib/vendor/twig/twig/doc/filters/date.rst | 82 + .../twig/twig/doc/filters/date_modify.rst | 20 + .../vendor/twig/twig/doc/filters/default.rst | 33 + .../vendor/twig/twig/doc/filters/escape.rst | 119 + .../vendor/twig/twig/doc/filters/filter.rst | 55 + .../vendor/twig/twig/doc/filters/first.rst | 22 + .../vendor/twig/twig/doc/filters/format.rst | 16 + .../twig/twig/doc/filters/format_currency.rst | 74 + .../twig/twig/doc/filters/format_date.rst | 32 + .../twig/twig/doc/filters/format_datetime.rst | 68 + .../twig/twig/doc/filters/format_number.rst | 118 + .../twig/twig/doc/filters/format_time.rst | 32 + .../twig/doc/filters/html_to_markdown.rst | 66 + .../vendor/twig/twig/doc/filters/index.rst | 59 + .../twig/twig/doc/filters/inky_to_html.rst | 40 + .../twig/twig/doc/filters/inline_css.rst | 64 + .../lib/vendor/twig/twig/doc/filters/join.rst | 32 + .../twig/twig/doc/filters/json_encode.rst | 23 + .../lib/vendor/twig/twig/doc/filters/keys.rst | 11 + .../twig/twig/doc/filters/language_name.rst | 45 + .../lib/vendor/twig/twig/doc/filters/last.rst | 22 + .../vendor/twig/twig/doc/filters/length.rst | 19 + .../twig/twig/doc/filters/locale_name.rst | 45 + .../vendor/twig/twig/doc/filters/lower.rst | 10 + .../lib/vendor/twig/twig/doc/filters/map.rst | 34 + .../twig/doc/filters/markdown_to_html.rst | 65 + .../vendor/twig/twig/doc/filters/merge.rst | 48 + .../vendor/twig/twig/doc/filters/nl2br.rst | 19 + .../twig/twig/doc/filters/number_format.rst | 53 + .../lib/vendor/twig/twig/doc/filters/raw.rst | 12 + .../vendor/twig/twig/doc/filters/reduce.rst | 29 + .../vendor/twig/twig/doc/filters/replace.rst | 25 + .../vendor/twig/twig/doc/filters/reverse.rst | 44 + .../vendor/twig/twig/doc/filters/round.rst | 34 + .../vendor/twig/twig/doc/filters/slice.rst | 68 + .../lib/vendor/twig/twig/doc/filters/sort.rst | 45 + .../twig/twig/doc/filters/spaceless.rst | 56 + .../vendor/twig/twig/doc/filters/split.rst | 50 + .../twig/twig/doc/filters/striptags.rst | 29 + .../twig/twig/doc/filters/timezone_name.rst | 44 + .../vendor/twig/twig/doc/filters/title.rst | 11 + .../lib/vendor/twig/twig/doc/filters/trim.rst | 39 + .../lib/vendor/twig/twig/doc/filters/u.rst | 78 + .../vendor/twig/twig/doc/filters/upper.rst | 10 + .../twig/twig/doc/filters/url_encode.rst | 22 + .../twig/twig/doc/functions/attribute.rst | 23 + .../vendor/twig/twig/doc/functions/block.rst | 35 + .../twig/twig/doc/functions/constant.rst | 23 + .../twig/doc/functions/country_timezones.rst | 30 + .../vendor/twig/twig/doc/functions/cycle.rst | 28 + .../vendor/twig/twig/doc/functions/date.rst | 46 + .../vendor/twig/twig/doc/functions/dump.rst | 66 + .../twig/twig/doc/functions/html_classes.rst | 33 + .../twig/twig/doc/functions/include.rst | 77 + .../vendor/twig/twig/doc/functions/index.rst | 22 + .../vendor/twig/twig/doc/functions/max.rst | 17 + .../vendor/twig/twig/doc/functions/min.rst | 17 + .../vendor/twig/twig/doc/functions/parent.rst | 20 + .../vendor/twig/twig/doc/functions/random.rst | 27 + .../vendor/twig/twig/doc/functions/range.rst | 58 + .../vendor/twig/twig/doc/functions/source.rst | 26 + .../doc/functions/template_from_string.rst | 37 + .../lib/vendor/twig/twig/doc/index.rst | 19 + .../lib/vendor/twig/twig/doc/installation.rst | 10 + .../lib/vendor/twig/twig/doc/internals.rst | 140 + .../lib/vendor/twig/twig/doc/intro.rst | 70 + .../lib/vendor/twig/twig/doc/recipes.rst | 527 + .../lib/vendor/twig/twig/doc/tags/apply.rst | 20 + .../vendor/twig/twig/doc/tags/autoescape.rst | 61 + .../lib/vendor/twig/twig/doc/tags/block.rst | 11 + .../vendor/twig/twig/doc/tags/deprecated.rst | 27 + .../lib/vendor/twig/twig/doc/tags/do.rst | 9 + .../lib/vendor/twig/twig/doc/tags/embed.rst | 175 + .../lib/vendor/twig/twig/doc/tags/extends.rst | 263 + .../lib/vendor/twig/twig/doc/tags/flush.rst | 14 + .../lib/vendor/twig/twig/doc/tags/for.rst | 141 + .../lib/vendor/twig/twig/doc/tags/from.rst | 6 + .../lib/vendor/twig/twig/doc/tags/if.rst | 79 + .../lib/vendor/twig/twig/doc/tags/import.rst | 6 + .../lib/vendor/twig/twig/doc/tags/include.rst | 110 + .../lib/vendor/twig/twig/doc/tags/index.rst | 25 + .../lib/vendor/twig/twig/doc/tags/macro.rst | 143 + .../lib/vendor/twig/twig/doc/tags/sandbox.rst | 30 + .../lib/vendor/twig/twig/doc/tags/set.rst | 78 + .../lib/vendor/twig/twig/doc/tags/use.rst | 117 + .../vendor/twig/twig/doc/tags/verbatim.rst | 16 + .../lib/vendor/twig/twig/doc/tags/with.rst | 41 + .../lib/vendor/twig/twig/doc/templates.rst | 861 + .../vendor/twig/twig/doc/tests/constant.rst | 19 + .../vendor/twig/twig/doc/tests/defined.rst | 30 + .../twig/twig/doc/tests/divisibleby.rst | 10 + .../lib/vendor/twig/twig/doc/tests/empty.rst | 18 + .../lib/vendor/twig/twig/doc/tests/even.rst | 10 + .../lib/vendor/twig/twig/doc/tests/index.rst | 15 + .../vendor/twig/twig/doc/tests/iterable.rst | 16 + .../lib/vendor/twig/twig/doc/tests/null.rst | 12 + .../lib/vendor/twig/twig/doc/tests/odd.rst | 10 + .../lib/vendor/twig/twig/doc/tests/sameas.rst | 11 + .../lib/vendor/twig/twig/drupal_test.sh | 52 + .../twig/twig/src/Cache/CacheInterface.php | 46 + .../twig/twig/src/Cache/FilesystemCache.php | 87 + .../vendor/twig/twig/src/Cache/NullCache.php | 38 + .../lib/vendor/twig/twig/src/Compiler.php | 214 + .../lib/vendor/twig/twig/src/Environment.php | 816 + .../lib/vendor/twig/twig/src/Error/Error.php | 227 + .../twig/twig/src/Error/LoaderError.php | 21 + .../twig/twig/src/Error/RuntimeError.php | 22 + .../twig/twig/src/Error/SyntaxError.php | 46 + .../vendor/twig/twig/src/ExpressionParser.php | 807 + .../twig/src/Extension/AbstractExtension.php | 45 + .../twig/twig/src/Extension/CoreExtension.php | 1594 + .../twig/src/Extension/DebugExtension.php | 64 + .../twig/src/Extension/EscaperExtension.php | 418 + .../twig/src/Extension/ExtensionInterface.php | 68 + .../twig/src/Extension/GlobalsInterface.php | 25 + .../twig/src/Extension/OptimizerExtension.php | 29 + .../twig/src/Extension/ProfilerExtension.php | 52 + .../Extension/RuntimeExtensionInterface.php | 19 + .../twig/src/Extension/SandboxExtension.php | 123 + .../twig/src/Extension/StagingExtension.php | 100 + .../src/Extension/StringLoaderExtension.php | 42 + .../lib/vendor/twig/twig/src/ExtensionSet.php | 438 + .../src/FileExtensionEscapingStrategy.php | 60 + .../lib/vendor/twig/twig/src/Lexer.php | 497 + .../twig/twig/src/Loader/ArrayLoader.php | 78 + .../twig/twig/src/Loader/ChainLoader.php | 119 + .../twig/twig/src/Loader/FilesystemLoader.php | 283 + .../twig/twig/src/Loader/LoaderInterface.php | 49 + .../lib/vendor/twig/twig/src/Markup.php | 44 + .../twig/twig/src/Node/AutoEscapeNode.php | 38 + .../vendor/twig/twig/src/Node/BlockNode.php | 44 + .../twig/twig/src/Node/BlockReferenceNode.php | 36 + .../vendor/twig/twig/src/Node/BodyNode.php | 21 + .../twig/twig/src/Node/CheckSecurityNode.php | 83 + .../twig/twig/src/Node/CheckToStringNode.php | 45 + .../twig/twig/src/Node/DeprecatedNode.php | 53 + .../lib/vendor/twig/twig/src/Node/DoNode.php | 38 + .../vendor/twig/twig/src/Node/EmbedNode.php | 48 + .../Node/Expression/AbstractExpression.php | 24 + .../src/Node/Expression/ArrayExpression.php | 85 + .../Expression/ArrowFunctionExpression.php | 64 + .../Node/Expression/AssignNameExpression.php | 27 + .../Node/Expression/Binary/AbstractBinary.php | 42 + .../src/Node/Expression/Binary/AddBinary.php | 23 + .../src/Node/Expression/Binary/AndBinary.php | 23 + .../Expression/Binary/BitwiseAndBinary.php | 23 + .../Expression/Binary/BitwiseOrBinary.php | 23 + .../Expression/Binary/BitwiseXorBinary.php | 23 + .../Node/Expression/Binary/ConcatBinary.php | 23 + .../src/Node/Expression/Binary/DivBinary.php | 23 + .../Node/Expression/Binary/EndsWithBinary.php | 35 + .../Node/Expression/Binary/EqualBinary.php | 39 + .../Node/Expression/Binary/FloorDivBinary.php | 29 + .../Node/Expression/Binary/GreaterBinary.php | 39 + .../Expression/Binary/GreaterEqualBinary.php | 39 + .../src/Node/Expression/Binary/InBinary.php | 33 + .../src/Node/Expression/Binary/LessBinary.php | 39 + .../Expression/Binary/LessEqualBinary.php | 39 + .../Node/Expression/Binary/MatchesBinary.php | 33 + .../src/Node/Expression/Binary/ModBinary.php | 23 + .../src/Node/Expression/Binary/MulBinary.php | 23 + .../Node/Expression/Binary/NotEqualBinary.php | 39 + .../Node/Expression/Binary/NotInBinary.php | 33 + .../src/Node/Expression/Binary/OrBinary.php | 23 + .../Node/Expression/Binary/PowerBinary.php | 22 + .../Node/Expression/Binary/RangeBinary.php | 33 + .../Expression/Binary/SpaceshipBinary.php | 22 + .../Expression/Binary/StartsWithBinary.php | 35 + .../src/Node/Expression/Binary/SubBinary.php | 23 + .../Expression/BlockReferenceExpression.php | 86 + .../src/Node/Expression/CallExpression.php | 310 + .../Node/Expression/ConditionalExpression.php | 36 + .../Node/Expression/ConstantExpression.php | 28 + .../Node/Expression/Filter/DefaultFilter.php | 52 + .../src/Node/Expression/FilterExpression.php | 40 + .../Node/Expression/FunctionExpression.php | 43 + .../src/Node/Expression/GetAttrExpression.php | 87 + .../twig/src/Node/Expression/InlinePrint.php | 35 + .../Node/Expression/MethodCallExpression.php | 62 + .../src/Node/Expression/NameExpression.php | 97 + .../Expression/NullCoalesceExpression.php | 60 + .../src/Node/Expression/ParentExpression.php | 46 + .../Node/Expression/TempNameExpression.php | 31 + .../src/Node/Expression/Test/ConstantTest.php | 49 + .../src/Node/Expression/Test/DefinedTest.php | 74 + .../Node/Expression/Test/DivisiblebyTest.php | 36 + .../src/Node/Expression/Test/EvenTest.php | 35 + .../src/Node/Expression/Test/NullTest.php | 34 + .../twig/src/Node/Expression/Test/OddTest.php | 35 + .../src/Node/Expression/Test/SameasTest.php | 34 + .../src/Node/Expression/TestExpression.php | 42 + .../Node/Expression/Unary/AbstractUnary.php | 34 + .../src/Node/Expression/Unary/NegUnary.php | 23 + .../src/Node/Expression/Unary/NotUnary.php | 23 + .../src/Node/Expression/Unary/PosUnary.php | 23 + .../Node/Expression/VariadicExpression.php | 24 + .../vendor/twig/twig/src/Node/FlushNode.php | 35 + .../vendor/twig/twig/src/Node/ForLoopNode.php | 49 + .../lib/vendor/twig/twig/src/Node/ForNode.php | 107 + .../lib/vendor/twig/twig/src/Node/IfNode.php | 70 + .../vendor/twig/twig/src/Node/ImportNode.php | 63 + .../vendor/twig/twig/src/Node/IncludeNode.php | 106 + .../vendor/twig/twig/src/Node/MacroNode.php | 113 + .../vendor/twig/twig/src/Node/ModuleNode.php | 464 + .../lib/vendor/twig/twig/src/Node/Node.php | 175 + .../twig/src/Node/NodeCaptureInterface.php | 21 + .../twig/src/Node/NodeOutputInterface.php | 21 + .../vendor/twig/twig/src/Node/PrintNode.php | 39 + .../vendor/twig/twig/src/Node/SandboxNode.php | 45 + .../lib/vendor/twig/twig/src/Node/SetNode.php | 105 + .../vendor/twig/twig/src/Node/TextNode.php | 38 + .../vendor/twig/twig/src/Node/WithNode.php | 70 + .../vendor/twig/twig/src/NodeTraverser.php | 76 + .../src/NodeVisitor/AbstractNodeVisitor.php | 49 + .../src/NodeVisitor/EscaperNodeVisitor.php | 206 + .../MacroAutoImportNodeVisitor.php | 72 + .../src/NodeVisitor/NodeVisitorInterface.php | 46 + .../src/NodeVisitor/OptimizerNodeVisitor.php | 215 + .../NodeVisitor/SafeAnalysisNodeVisitor.php | 157 + .../src/NodeVisitor/SandboxNodeVisitor.php | 132 + .../lib/vendor/twig/twig/src/Parser.php | 360 + .../twig/src/Profiler/Dumper/BaseDumper.php | 63 + .../src/Profiler/Dumper/BlackfireDumper.php | 72 + .../twig/src/Profiler/Dumper/HtmlDumper.php | 47 + .../twig/src/Profiler/Dumper/TextDumper.php | 35 + .../src/Profiler/Node/EnterProfileNode.php | 42 + .../src/Profiler/Node/LeaveProfileNode.php | 36 + .../NodeVisitor/ProfilerNodeVisitor.php | 76 + .../vendor/twig/twig/src/Profiler/Profile.php | 181 + .../RuntimeLoader/ContainerRuntimeLoader.php | 37 + .../RuntimeLoader/FactoryRuntimeLoader.php | 41 + .../RuntimeLoader/RuntimeLoaderInterface.php | 27 + .../twig/twig/src/Sandbox/SecurityError.php | 23 + .../Sandbox/SecurityNotAllowedFilterError.php | 33 + .../SecurityNotAllowedFunctionError.php | 33 + .../Sandbox/SecurityNotAllowedMethodError.php | 40 + .../SecurityNotAllowedPropertyError.php | 40 + .../Sandbox/SecurityNotAllowedTagError.php | 33 + .../twig/twig/src/Sandbox/SecurityPolicy.php | 126 + .../src/Sandbox/SecurityPolicyInterface.php | 35 + .../lib/vendor/twig/twig/src/Source.php | 51 + .../lib/vendor/twig/twig/src/Template.php | 424 + .../vendor/twig/twig/src/TemplateWrapper.php | 109 + .../twig/src/Test/IntegrationTestCase.php | 265 + .../twig/twig/src/Test/NodeTestCase.php | 65 + .../lib/vendor/twig/twig/src/Token.php | 178 + .../src/TokenParser/AbstractTokenParser.php | 32 + .../twig/src/TokenParser/ApplyTokenParser.php | 58 + .../src/TokenParser/AutoEscapeTokenParser.php | 56 + .../twig/src/TokenParser/BlockTokenParser.php | 76 + .../src/TokenParser/DeprecatedTokenParser.php | 41 + .../twig/src/TokenParser/DoTokenParser.php | 36 + .../twig/src/TokenParser/EmbedTokenParser.php | 71 + .../src/TokenParser/ExtendsTokenParser.php | 50 + .../twig/src/TokenParser/FlushTokenParser.php | 36 + .../twig/src/TokenParser/ForTokenParser.php | 77 + .../twig/src/TokenParser/FromTokenParser.php | 64 + .../twig/src/TokenParser/IfTokenParser.php | 87 + .../src/TokenParser/ImportTokenParser.php | 42 + .../src/TokenParser/IncludeTokenParser.php | 67 + .../twig/src/TokenParser/MacroTokenParser.php | 64 + .../src/TokenParser/SandboxTokenParser.php | 64 + .../twig/src/TokenParser/SetTokenParser.php | 71 + .../src/TokenParser/TokenParserInterface.php | 46 + .../twig/src/TokenParser/UseTokenParser.php | 71 + .../twig/src/TokenParser/WithTokenParser.php | 54 + .../lib/vendor/twig/twig/src/TokenStream.php | 132 + .../lib/vendor/twig/twig/src/TwigFilter.php | 134 + .../lib/vendor/twig/twig/src/TwigFunction.php | 122 + .../lib/vendor/twig/twig/src/TwigTest.php | 94 + .../twig/src/Util/DeprecationCollector.php | 77 + .../twig/src/Util/TemplateDirIterator.php | 28 + timeclock_plandutravail_lu/pages/appkey.php | 15 - .../pages/screensaver.php | 4 - .../{pages => tmpl}/.htaccess | 0 timeclock_plandutravail_lu/tmpl/index.html | 23 + .../tmpl/module/index.html | 9 + .../tmpl/module/js/app.js | 92 + .../lang/en.js => tmpl/module/js/request.js} | 0 .../tmpl/module/js/timecalc.js | 129 + .../tmpl/module/js/timeclock.js | 359 + .../tmpl/panels/appkey.html | 31 + .../panels/badgesadmin.html} | 0 .../nfcscan.php => tmpl/panels/nfcscan.html} | 0 .../pincode.php => tmpl/panels/pincode.html} | 0 .../tmpl/panels/screensaver.html | 4 + .../status.php => tmpl/panels/status.html} | 0 .../panels/timetrack.html} | 0 .../panels/userlist.html} | 0 392 files changed, 66559 insertions(+), 664 deletions(-) create mode 100644 app_plandutravail_lu/tmpl/module/admin/dlg_period.html create mode 100644 hourtrax/install/bin/NFCRead.jar create mode 100644 hourtrax/install/bin/nfcread create mode 100644 hourtrax/src/lu/plandutravail/NFCRead$ByteCastException.class create mode 100644 hourtrax/src/lu/plandutravail/NFCRead$InstructionFailedException.class create mode 100644 hourtrax/src/lu/plandutravail/NFCRead$TerminalNotFoundException.class create mode 100644 hourtrax/src/lu/plandutravail/NFCRead.class create mode 100644 hourtrax/src/lu/plandutravail/NFCRead.java create mode 100644 timeclock_plandutravail_lu/.htaccess delete mode 100644 timeclock_plandutravail_lu/js/lang/de.js delete mode 100644 timeclock_plandutravail_lu/js/lang/fr.js create mode 100644 timeclock_plandutravail_lu/lib/composer.json create mode 100644 timeclock_plandutravail_lu/lib/composer.lock create mode 100644 timeclock_plandutravail_lu/lib/vendor/autoload.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/ClassLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/InstalledVersions.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/LICENSE create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_classmap.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_files.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_namespaces.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_psr4.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_real.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/autoload_static.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/installed.json create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/installed.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/composer/platform_check.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/Ctype.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/LICENSE create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/README.md create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/bootstrap.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/bootstrap80.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-ctype/composer.json create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/LICENSE create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/Mbstring.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/README.md create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/Resources/unidata/lowerCase.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/Resources/unidata/titleCaseRegexp.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/Resources/unidata/upperCase.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/bootstrap.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/bootstrap80.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/symfony/polyfill-mbstring/composer.json create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/.editorconfig create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/.gitattributes create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/.gitignore create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/.php_cs.dist create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/.travis.yml create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/CHANGELOG create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/LICENSE create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/README.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/composer.json create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/advanced.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/advanced_legacy.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/api.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/coding_standards.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/deprecated.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/abs.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/batch.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/capitalize.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/column.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/convert_encoding.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/country_name.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/currency_name.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/currency_symbol.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/data_uri.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/date.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/date_modify.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/default.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/escape.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/filter.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/first.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format_currency.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format_date.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format_datetime.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format_number.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/format_time.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/html_to_markdown.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/index.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/inky_to_html.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/inline_css.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/join.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/json_encode.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/keys.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/language_name.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/last.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/length.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/locale_name.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/lower.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/map.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/markdown_to_html.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/merge.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/nl2br.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/number_format.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/raw.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/reduce.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/replace.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/reverse.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/round.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/slice.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/sort.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/spaceless.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/split.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/striptags.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/timezone_name.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/title.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/trim.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/u.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/upper.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/filters/url_encode.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/attribute.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/block.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/constant.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/country_timezones.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/cycle.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/date.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/dump.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/html_classes.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/include.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/index.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/max.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/min.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/parent.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/random.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/range.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/source.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/functions/template_from_string.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/index.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/installation.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/internals.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/intro.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/recipes.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/apply.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/autoescape.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/block.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/deprecated.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/do.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/embed.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/extends.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/flush.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/for.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/from.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/if.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/import.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/include.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/index.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/macro.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/sandbox.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/set.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/use.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/verbatim.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tags/with.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/templates.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/constant.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/defined.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/divisibleby.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/empty.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/even.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/index.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/iterable.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/null.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/odd.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/doc/tests/sameas.rst create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/drupal_test.sh create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Cache/CacheInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Cache/FilesystemCache.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Cache/NullCache.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Compiler.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Environment.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Error/Error.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Error/LoaderError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Error/RuntimeError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Error/SyntaxError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/ExpressionParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/AbstractExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/CoreExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/DebugExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/EscaperExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/ExtensionInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/GlobalsInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/OptimizerExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/ProfilerExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/RuntimeExtensionInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/SandboxExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/StagingExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Extension/StringLoaderExtension.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/ExtensionSet.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/FileExtensionEscapingStrategy.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Lexer.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Loader/ArrayLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Loader/ChainLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Loader/FilesystemLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Loader/LoaderInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Markup.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/AutoEscapeNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/BlockNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/BlockReferenceNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/BodyNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/CheckSecurityNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/CheckToStringNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/DeprecatedNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/DoNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/EmbedNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/AbstractExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/ArrayExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/ArrowFunctionExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/AssignNameExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/AbstractBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/AddBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/AndBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/BitwiseAndBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/BitwiseOrBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/BitwiseXorBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/ConcatBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/DivBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/EndsWithBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/EqualBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/FloorDivBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/GreaterBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/GreaterEqualBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/InBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/LessBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/LessEqualBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/MatchesBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/ModBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/MulBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/NotEqualBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/NotInBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/OrBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/PowerBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/RangeBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/SpaceshipBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/StartsWithBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Binary/SubBinary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/BlockReferenceExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/CallExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/ConditionalExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/ConstantExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Filter/DefaultFilter.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/FilterExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/FunctionExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/GetAttrExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/InlinePrint.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/MethodCallExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/NameExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/NullCoalesceExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/ParentExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/TempNameExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/ConstantTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/DefinedTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/DivisiblebyTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/EvenTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/NullTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/OddTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Test/SameasTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/TestExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Unary/AbstractUnary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Unary/NegUnary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Unary/NotUnary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/Unary/PosUnary.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Expression/VariadicExpression.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/FlushNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/ForLoopNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/ForNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/IfNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/ImportNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/IncludeNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/MacroNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/ModuleNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/Node.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/NodeCaptureInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/NodeOutputInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/PrintNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/SandboxNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/SetNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/TextNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Node/WithNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeTraverser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/AbstractNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/EscaperNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/MacroAutoImportNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/NodeVisitorInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/OptimizerNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/SafeAnalysisNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/NodeVisitor/SandboxNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Parser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Dumper/BaseDumper.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Dumper/BlackfireDumper.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Dumper/HtmlDumper.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Dumper/TextDumper.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Node/EnterProfileNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Node/LeaveProfileNode.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/NodeVisitor/ProfilerNodeVisitor.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Profiler/Profile.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/RuntimeLoader/ContainerRuntimeLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/RuntimeLoader/FactoryRuntimeLoader.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/RuntimeLoader/RuntimeLoaderInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityNotAllowedFilterError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityNotAllowedFunctionError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityNotAllowedMethodError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityNotAllowedPropertyError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityNotAllowedTagError.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityPolicy.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Sandbox/SecurityPolicyInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Source.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Template.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TemplateWrapper.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Test/IntegrationTestCase.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Test/NodeTestCase.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Token.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/AbstractTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/ApplyTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/AutoEscapeTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/BlockTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/DeprecatedTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/DoTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/EmbedTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/ExtendsTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/FlushTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/ForTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/FromTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/IfTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/ImportTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/IncludeTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/MacroTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/SandboxTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/SetTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/TokenParserInterface.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/UseTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenParser/WithTokenParser.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TokenStream.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TwigFilter.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TwigFunction.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/TwigTest.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Util/DeprecationCollector.php create mode 100644 timeclock_plandutravail_lu/lib/vendor/twig/twig/src/Util/TemplateDirIterator.php delete mode 100644 timeclock_plandutravail_lu/pages/appkey.php delete mode 100644 timeclock_plandutravail_lu/pages/screensaver.php rename timeclock_plandutravail_lu/{pages => tmpl}/.htaccess (100%) create mode 100644 timeclock_plandutravail_lu/tmpl/index.html create mode 100644 timeclock_plandutravail_lu/tmpl/module/index.html create mode 100644 timeclock_plandutravail_lu/tmpl/module/js/app.js rename timeclock_plandutravail_lu/{js/lang/en.js => tmpl/module/js/request.js} (100%) create mode 100644 timeclock_plandutravail_lu/tmpl/module/js/timecalc.js create mode 100644 timeclock_plandutravail_lu/tmpl/module/js/timeclock.js create mode 100644 timeclock_plandutravail_lu/tmpl/panels/appkey.html rename timeclock_plandutravail_lu/{pages/badgesadmin.php => tmpl/panels/badgesadmin.html} (100%) rename timeclock_plandutravail_lu/{pages/nfcscan.php => tmpl/panels/nfcscan.html} (100%) rename timeclock_plandutravail_lu/{pages/pincode.php => tmpl/panels/pincode.html} (100%) create mode 100644 timeclock_plandutravail_lu/tmpl/panels/screensaver.html rename timeclock_plandutravail_lu/{pages/status.php => tmpl/panels/status.html} (100%) rename timeclock_plandutravail_lu/{pages/timetrack.php => tmpl/panels/timetrack.html} (100%) rename timeclock_plandutravail_lu/{pages/userlist.php => tmpl/panels/userlist.html} (100%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4fe9fb3e..08449bf9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { + "*.html": "twig", "*.html.mustache": "html", "*.js.mustache": "javascript" }, diff --git a/.vscode/sftp.json b/.vscode/sftp.json index 6d5b8dd7..a067db17 100644 --- a/.vscode/sftp.json +++ b/.vscode/sftp.json @@ -1,7 +1,7 @@ { "name": "POT-APP", "scheme": "sftp", - "context": "app", + "context": "app_plandutravail_lu", "host": "www422.your-server.de", "username": "dksalu", "password": "JajU154BbR96ZfPX", diff --git a/app_plandutravail_lu/data/schemata/defaultcompany.schema.sql b/app_plandutravail_lu/data/schemata/defaultcompany.schema.sql index 02a9ef90..a9aa2716 100644 --- a/app_plandutravail_lu/data/schemata/defaultcompany.schema.sql +++ b/app_plandutravail_lu/data/schemata/defaultcompany.schema.sql @@ -1,10 +1,14 @@ CREATE SCHEMA %%NEWSCHEMA%%; -CREATE TABLE %%NEWSCHEMA%%.clientconfig ( - id text NOT NULL, - value text -); +CREATE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable() RETURNS trigger + LANGUAGE plpgsql + AS $$ + begin + new.modified = now(); + RETURN NEW; + END; +$$; CREATE TABLE %%NEWSCHEMA%%.companies ( company text, @@ -62,7 +66,10 @@ CREATE TABLE %%NEWSCHEMA%%.reportperiod ( created timestamp without time zone DEFAULT now(), periodtype text, subinterval text, - viewpartunempl boolean + viewpartunempl boolean, + periodlength integer, + periodunit text, + sector text ); CREATE TABLE %%NEWSCHEMA%%.staff ( @@ -105,7 +112,10 @@ CREATE TABLE %%NEWSCHEMA%%.staff ( sunstart time without time zone, sunend time without time zone, nfcuid text, - color text + color text, + pincode text, + username text, + userpassword text ); CREATE TABLE %%NEWSCHEMA%%.staffcontract ( @@ -155,21 +165,24 @@ CREATE TABLE %%NEWSCHEMA%%.staffreportperiod ( status44hcount integer DEFAULT 0, last44date date, lastsundate date, + itmcontracthours interval DEFAULT '00:00:00'::interval, + maxdays integer DEFAULT 0, avgtotalweekhours interval, - itmcontracthours interval, - maxdays integer, vacancyill interval, vacancynormal interval, - hourstotransfercalc interval, - hourstotransfer interval, isvalidated boolean, - payedhours0 interval, - payedhours40calc interval, payedhours40 interval, + payedhours40calc interval, + hourstotransfer interval, + hourstotransfercalc interval, + payedhours0 interval, + unpayedprevious interval, vacancyextra interval, partunemplhours interval, - otherpaidhours interval(6), - nighthours interval + otherpaidhours interval, + nighthours interval, + nighthourstotransfer interval, + nighthourstotransfercalc interval ); CREATE TABLE %%NEWSCHEMA%%.staffreportperioddays ( @@ -307,9 +320,6 @@ CREATE TABLE %%NEWSCHEMA%%.worktimes ( maxhourspercents numeric(4,2) ); -ALTER TABLE ONLY %%NEWSCHEMA%%.clientconfig - ADD CONSTRAINT clientconfig_pkey PRIMARY KEY (id); - ALTER TABLE ONLY %%NEWSCHEMA%%.companies ADD CONSTRAINT companies_pkey PRIMARY KEY (id); @@ -328,9 +338,6 @@ ALTER TABLE ONLY %%NEWSCHEMA%%.staffcontract ALTER TABLE ONLY %%NEWSCHEMA%%.staffgroups ADD CONSTRAINT staffgroups_pkey PRIMARY KEY (id); -ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperiod - ADD CONSTRAINT staffreportperiod_id_reportperiod_id_staff_key UNIQUE (id_reportperiod, id_staff); - ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperiod ADD CONSTRAINT staffreportperiod_pkey PRIMARY KEY (id); @@ -361,9 +368,6 @@ ALTER TABLE ONLY %%NEWSCHEMA%%.vacancydays ALTER TABLE ONLY %%NEWSCHEMA%%.vacancytypes ADD CONSTRAINT vacancytypes_pkey PRIMARY KEY (id); -ALTER TABLE ONLY %%NEWSCHEMA%%.workplandays - ADD CONSTRAINT workplandays_id_workplan_weekday_key UNIQUE (id_workplan, weekday); - ALTER TABLE ONLY %%NEWSCHEMA%%.workplandays ADD CONSTRAINT workplandays_pkey PRIMARY KEY (id); @@ -376,3 +380,45 @@ ALTER TABLE ONLY %%NEWSCHEMA%%.workplans ALTER TABLE ONLY %%NEWSCHEMA%%.worktimes ADD CONSTRAINT worktimes_pkey PRIMARY KEY (id); +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_reportperiod BEFORE UPDATE ON %%NEWSCHEMA%%.reportperiod FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_staff BEFORE UPDATE ON %%NEWSCHEMA%%.staff FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_staffcontract BEFORE UPDATE ON %%NEWSCHEMA%%.staffcontract FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_staffgroups BEFORE UPDATE ON %%NEWSCHEMA%%.staffgroups FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_staffreportperiod BEFORE UPDATE ON %%NEWSCHEMA%%.staffreportperiod FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_staffreportperioddays BEFORE UPDATE ON %%NEWSCHEMA%%.staffreportperioddays FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_timetracker BEFORE UPDATE ON %%NEWSCHEMA%%.timetracker FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_vacancydays BEFORE UPDATE ON %%NEWSCHEMA%%.vacancydays FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_vacancytypes BEFORE UPDATE ON %%NEWSCHEMA%%.vacancytypes FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_workplandays BEFORE UPDATE ON %%NEWSCHEMA%%.workplandays FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_workplans BEFORE UPDATE ON %%NEWSCHEMA%%.workplans FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +CREATE TRIGGER trg_%%NEWSCHEMA%%_before_upd_worktimes BEFORE UPDATE ON %%NEWSCHEMA%%.worktimes FOR EACH ROW EXECUTE FUNCTION %%NEWSCHEMA%%.trg_before_upd_schematable(); + +ALTER TABLE ONLY %%NEWSCHEMA%%.staffcontract + ADD CONSTRAINT staffcontract_fk FOREIGN KEY (id_staff) REFERENCES %%NEWSCHEMA%%.staff(id); + +ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperiod + ADD CONSTRAINT staffreportperiod_fk FOREIGN KEY (id_staff) REFERENCES %%NEWSCHEMA%%.staff(id); + +ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperiod + ADD CONSTRAINT staffreportperiod_fk_1 FOREIGN KEY (id_reportperiod) REFERENCES %%NEWSCHEMA%%.reportperiod(id); + +ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperioddays + ADD CONSTRAINT staffworkplan_fk FOREIGN KEY (id_staff) REFERENCES %%NEWSCHEMA%%.staff(id); + +ALTER TABLE ONLY %%NEWSCHEMA%%.staffreportperioddays + ADD CONSTRAINT staffworkplan_fk_1 FOREIGN KEY (id_staffgroup) REFERENCES %%NEWSCHEMA%%.staffgroups(id); + +ALTER TABLE ONLY %%NEWSCHEMA%%.workplandays + ADD CONSTRAINT workplandays_fk FOREIGN KEY (id_workplan) REFERENCES %%NEWSCHEMA%%.workplans(id); + diff --git a/app_plandutravail_lu/db.php b/app_plandutravail_lu/db.php index f34ee9f4..225333ee 100644 --- a/app_plandutravail_lu/db.php +++ b/app_plandutravail_lu/db.php @@ -261,7 +261,7 @@ } $usql = "UPDATE ".$schema.".".$db->securetext($p["table"])." SET ".$db->securetext($p["column"])."=".$val." WHERE id='".$db->securetext($p["id"])."';"; - file_put_contents("log/sqlx.log",date('Y-m-d H:i:s').":".$usql."\n", FILE_APPEND); + //file_put_contents("log/sqlx.log",date('Y-m-d H:i:s').":".$usql."\n", FILE_APPEND); $html["result"] = $db->exec($usql); } } @@ -388,7 +388,7 @@ } elseif ($p["fn"] == "workplan_saveday"){ #print STDERR Dumper($p["workplanday"]); //error_log("saveday!!!!"); - $html["result"] = $wp->saveWorkplanDay($schema,$p["workplanday"],$db->securetext($p["id"])); + //$html["result"] = $wp->saveWorkplanDay($schema,$p["workplanday"],$db->securetext($p["id"])); } } elseif (preg_match('/del_worktimes$/',$p["fn"])) { //error_log("Pregmatch del_worktimes"); diff --git a/app_plandutravail_lu/lib/POT/Company.php b/app_plandutravail_lu/lib/POT/Company.php index b2451690..367e1e3a 100644 --- a/app_plandutravail_lu/lib/POT/Company.php +++ b/app_plandutravail_lu/lib/POT/Company.php @@ -86,7 +86,7 @@ function deleteStaffGroup($schema,$stgid){ } function getCompaniesData(){ - $cpall = $this->dbh->queryarray("SELECT * FROM public.companies WHERE isdeleted IS NULL;"); + $cpall = $this->dbh->queryarray("SELECT *,'' || COALESCE(coalesce(company,'') || ' ', '') || '
' || ''|| coalesce(datasetname,'') || '' AS listname FROM public.companies WHERE isdeleted IS NULL order by portal,company;"); return $cpall; } diff --git a/app_plandutravail_lu/lib/POT/Period.php b/app_plandutravail_lu/lib/POT/Period.php index eba6430e..d26e73b5 100644 --- a/app_plandutravail_lu/lib/POT/Period.php +++ b/app_plandutravail_lu/lib/POT/Period.php @@ -47,7 +47,7 @@ class Period{ } public function getPeriods($schema){ - $ret = $this->dbh->queryarray("SELECT id,startdate,enddate,id_parentreportperiod,periodtype,subinterval,viewpartunempl, to_char(startdate,'MM/YY') || ' - ' || to_char(enddate,'MM/YY') as dspperiod FROM ".$schema.".reportperiod rp ORDER BY startdate, enddate;"); + $ret = $this->dbh->queryarray("SELECT id,startdate,enddate,id_parentreportperiod,periodtype,subinterval,viewpartunempl,periodlength,periodunit,sector,periodlength || ' ' || periodunit as dspunit, to_char(startdate,'MM/YY') || ' - ' || to_char(enddate,'MM/YY') as dspperiod FROM ".$schema.".reportperiod rp ORDER BY startdate, enddate;"); return $ret; } @@ -438,13 +438,15 @@ to_char(mindailyrecuperation,'HH24:MI') as mindailyrecuperation from ".$schema." public function getPeriodDayLimits($schema,$daydate,$id_staff){ //date('".$daydate."') date, vidstaff //get contract limit + $csql = "select weekhours from ".$schema.".staffcontract where id_staff='".$id_staff."' and ((date('".$daydate."') between startdate and enddate) or (startdate <= date('".$daydate."') and enddate is null)) order by startdate,enddate nulls first limit 1;"; $before = $this->dbh->query("select daydate as datebefore,to_char(case when timestart2 is null then timeend1 else timeend2 end,'HH24:MI') as timeendbefore from ".$schema.".staffreportperioddays where id_staff= '".$id_staff."' and daydate < date('".$daydate."') and (timestart1 is not null or timestart2 is not null) order by daydate desc limit 1;"); //error_log("BEF:"."select daydate as datebefore,to_char(case when timestart2 is null then timeend1 else timeend2 end,'HH24:MI') as timeendbefore from ".$schema.".staffreportperioddays where id_staff= '".$id_staff."' and daydate < date('".$daydate."') and (timestart1 is not null or timestart2 is not null) order by daydate desc limit 1;".":".print_r($before,true)); $after = $this->dbh->query("select daydate as dateafter ,to_char(case when timestart1 is null then timestart2 else timestart1 end,'HH24:MI') as timestartafter from ".$schema.".staffreportperioddays where id_staff= '".$id_staff."' and daydate > date('".$daydate."') and (timestart1 is not null or timestart2 is not null) order by daydate asc limit 1;"); $contr = $this->dbh->query($csql); - // print STDERR "contr:\n".Dumper($contr)."\n"."\n===\n"; + //file_put_contents("log/sqlx.log","DayLimits 1: ".$csql."\n",FILE_APPEND); + // print STDERR "co ntr:\n".Dumper($contr)."\n"."\n===\n"; if ($contr["weekhours"] >= '40:00:00' ){ $sql = "select to_char(wk.maxdayhours,'HH24:MI') as maxdayhours, @@ -470,9 +472,10 @@ public function getPeriodDayLimits($schema,$daydate,$id_staff){ '".(($after && isset($after["dateafter"]))?$after["dateafter"]:'')."' as dateafter, '".(($after && isset($after["timestartafter"]))?$after["timestartafter"]:'')."' as timeafter from ".$schema.".worktimes wk where maxhourspercents is not null and maxdayhours <= '".$contr["weekhours"]."' LIMIT 1"; + //file_put_contents("log/sqlx.log","DayLimits <40: ".$sql."\n",FILE_APPEND); return $this->dbh->query($sql); } - return array(maxdayhours => "",maxweekhours => "",maxinterruptionhours => "",mindailyrecuperation => "",defaultweekhours => "",datebefore => "",timebefore => "",dateafter => "",timeafter => ""); + return array("maxdayhours" => "","maxweekhours" => "","maxinterruptionhours" => "","mindailyrecuperation" => "","defaultweekhours" => "","datebefore" => "","timebefore" => "","dateafter" => "","timeafter" => ""); } @@ -514,8 +517,8 @@ public function updatePeriodDaySums($schema,$dayids){ from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, - case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, - case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when pausestart1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end and pauseend1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end and pauseend2 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then timestart2::interval - timeend1::interval else null end as interruption, diff --git a/app_plandutravail_lu/lib/POT/Staff.php b/app_plandutravail_lu/lib/POT/Staff.php index 251f38ac..312ec69a 100644 --- a/app_plandutravail_lu/lib/POT/Staff.php +++ b/app_plandutravail_lu/lib/POT/Staff.php @@ -119,12 +119,13 @@ or date((select min(startdate) from ".$schema.".staffcontract where id_staff='". $checkhours = $this->dbh->query($chsql); $allmaxdays += intval($mdays["maxdays"]); $ghsumsql="select '".$gchours."'::interval + '".(($checkhours["itmcontracthours"] != "")?$checkhours["itmcontracthours"]:'00:00:00')."'::interval as gchours;"; + //file_put_contents("log/sqlx.log","CONTRACT HOURS:".$ghsumsql."\n",FILE_APPEND); $tmpgchours = $this->dbh->query($ghsumsql); - + $gchours = $tmpgchours["gchours"]; } $usql = "update ".$schema.".staffreportperiod set maxdays='".$allmaxdays."',contracthours='".$gchours."' where id_staff='".$id_staff."' and id_reportperiod='".$pp["id"]."';" ; - + //file_put_contents("log/sqlx.log","FINAL CONTRACT HOURS: ".$usql."\n",FILE_APPEND); $this->dbh->exec($usql); $period->updateStaffPeriod($schema,$pp["id"],$id_staff); } diff --git a/app_plandutravail_lu/lib/POT/TimeTracker.php b/app_plandutravail_lu/lib/POT/TimeTracker.php index 8be8e895..767408a2 100644 --- a/app_plandutravail_lu/lib/POT/TimeTracker.php +++ b/app_plandutravail_lu/lib/POT/TimeTracker.php @@ -208,7 +208,7 @@ public function setTracker($schema,$tracker,$data){ public function getWeekStamps($schema,$date){ - $xsql = "select yy.id_staff,yy.staffnumber,yy.staffname,yy.groupname,xx.cw, + $xsql = "select yy.id_staff,uu.id_reportperiod,yy.staffnumber,yy.staffname,yy.groupname,xx.cw, xx.montime,xx.monentry,xx.monexit, xx.tuetime,xx.tueentry,xx.tueexit, xx.wedtime,xx.wedentry,xx.wedexit, @@ -264,7 +264,7 @@ public function setTracker($schema,$tracker,$data){ ) zz on yy.id_staff=zz.id_staff left join ( select - pdu.id_staff, + pdu.id_staff,psplit.id_reportperiod, coalesce(case when position('-' in to_char(sum(pdu.dayhours)-sum(pdu.contracthours), 'HH24:MI'::text)) > 0 then '-' || replace(to_char(sum(pdu.dayhours)-sum(pdu.contracthours), 'HH24:MI'::text),'-','') else to_char(sum(pdu.dayhours)-sum(pdu.contracthours), 'HH24:MI'::text) end,'00:00') as hoursdiff, coalesce(to_char(sum(pdu.contracthours), 'HH24:MI'::text),'00:00') as contracthours, coalesce(to_char(sum(pdu.workhours), 'HH24:MI'::text),'00:00') as workhours, diff --git a/app_plandutravail_lu/lib/POT/Workplan.php b/app_plandutravail_lu/lib/POT/Workplan.php index 834e40c3..3936bf20 100644 --- a/app_plandutravail_lu/lib/POT/Workplan.php +++ b/app_plandutravail_lu/lib/POT/Workplan.php @@ -40,14 +40,14 @@ class Workplan{ function duplicateWorkplan($schema,$id){ $owk = $this->dbh->query("select workplan || ' Copy' as copyname from ".$schema.".workplans where id='".$id."';"); $wk = $this->dbh->query("insert into ".$schema.".workplans (workplan) values ('".$owk["copyname"]."') returning id;"); - $this->dbh->exec("insert into ".$schema.".workplandays (id_workplan, weekday, start1, end1, start2, end2,pausestart1,pauseend1,pausestart2,pauseend2, pause, weeknum) - select '".$wk["id"]."' as id_workplan, weekday,start1,end1,start2,end2,pausestart1,pauseend1,pausestart2,pauseend2,pause,weeknum from ".$schema.".workplandays where id_workplan = '".$id."';"); + $this->dbh->exec("insert into ".$schema.".workplandays (id_workplan, weekday, start1, end1, start2, end2,wpd1.pausestart1,pauseend1,pausestart2,pauseend2, pause, weeknum) + select '".$wk["id"]."' as id_workplan, weekday,start1,end1,start2,end2,wpd1.pausestart1,pauseend1,pausestart2,pauseend2,pause,weeknum from ".$schema.".workplandays where id_workplan = '".$id."';"); return $wk["id"]; } function replaceWorkplanDay($schema,$copyid,$pasteid){ $wk = $this->dbh->query("select * from ".$schema.".workplandays where id='".$copyid."';"); - $sql ="update ".$schema.".workplandays set start1=".$this->dbh->value($wk["start1"]).",end1=".$this->dbh->value($wk["end1"]).",start2=".$this->dbh->value($wk["start2"]).",end2=".$this->dbh->value($wk["end2"]).", pause=".$this->dbh->value($wk["pause"]).",pausestart1=".$this->dbh->value($wk["pausestart1"]).",pauseend1=".$this->dbh->value($wk["pauseend1"]).",pausestart2=".$this->dbh->value($wk["pausestart2"]).",pauseend2=".$this->dbh->value($wk["pauseend2"])." where id='".$pasteid."';"; + $sql ="update ".$schema.".workplandays set start1=".$this->dbh->value($wk["start1"]).",end1=".$this->dbh->value($wk["end1"]).",start2=".$this->dbh->value($wk["start2"]).",end2=".$this->dbh->value($wk["end2"]).", pause=".$this->dbh->value($wk["pause"]).",wpd1.pausestart1=".$this->dbh->value($wk["wpd1.pausestart1"]).",pauseend1=".$this->dbh->value($wk["pauseend1"]).",pausestart2=".$this->dbh->value($wk["pausestart2"]).",pauseend2=".$this->dbh->value($wk["pauseend2"])." where id='".$pasteid."';"; //print STDERR $sql.";\n"; $this->dbh->exec($sql); return 1; @@ -80,13 +80,9 @@ class Workplan{ to_char(wpd.pausestart1::interval, 'HH24:MI') AS pausestart1, to_char(wpd.pauseend1::interval, 'HH24:MI') AS pauseend1, to_char(wpd.pausestart2::interval, 'HH24:MI') AS pausestart2, - to_char(wpd.pauseend2::interval, 'HH24:MI') AS pauseend2,"; - //if ($payedpause["payedpauses"] != "1"){ - $sql .= "to_char( dt1+dt2-pdt1-pdt2, 'HH24:MI') AS dayhours,"; - //} else { - // $sql .= "to_char(dt1+dt2, 'HH24:MI') AS dayhours,"; - //} - $sql .=" COALESCE((to_char(wpd.pausestart1::interval, 'HH24:MI') || '-') || to_char(wpd.pauseend1::interval, 'HH24:MI'), '') || COALESCE((( + to_char(wpd.pauseend2::interval, 'HH24:MI') AS pauseend2, + to_char( dt1+dt2-pdt1-pdt2, 'HH24:MI') AS dayhours, + COALESCE((to_char(wpd.pausestart1::interval, 'HH24:MI') || '-') || to_char(wpd.pauseend1::interval, 'HH24:MI'), '') || COALESCE((( CASE WHEN wpd.pausestart1 IS NOT NULL THEN '
' ELSE '' END || to_char(wpd.pausestart2::interval, 'HH24:MI')) || '-') || to_char(wpd.pauseend2::interval, 'HH24:MI'), '') AS dsppausedata, COALESCE((to_char(wpd.start1::interval, 'HH24:MI') || '-') || to_char(wpd.end1::interval, 'HH24:MI'), '') || COALESCE((( @@ -94,11 +90,13 @@ class Workplan{ END || to_char(wpd.start2::interval, 'HH24:MI')) || '-') || to_char(wpd.end2::interval, 'HH24:MI'), '') AS dspworkdata from ( select wpd1.*,wp1.weekhours,wp1.workplan, - case when wpd1.start1 is not null and wpd1.end1 is not null then case when wpd1.end1 < wpd1.start1 then '24:00:00'::interval + wpd1.end1::interval -wpd1.start1::interval else wpd1.end1::interval - wpd1.start1::interval end else '00:00:00'::interval end as dt1, + case when wpd1.start1 is not null and wpd1.end1 is not null then case when wpd1.end1 < wpd1.start1 then '24:00:00'::interval + wpd1.end1::interval - wpd1.start1::interval else wpd1.end1::interval - wpd1.start1::interval end else '00:00:00'::interval end as dt1, case when wpd1.start2 is not null and wpd1.end2 is not null then case when wpd1.end2 < wpd1.start2 then '24:00:00'::interval + wpd1.end2::interval - wpd1.start2::interval else wpd1.end2::interval - wpd1.start2::interval end else '00:00:00'::interval end as dt2, - case when wpd1.pausestart1 is not null and wpd1.pauseend1 is not null then case when wpd1.pauseend1 < wpd1.pausestart1 then '24:00:00'::interval + wpd1.pauseend1::interval - wpd1.pausestart1::interval else wpd1.pauseend1::interval - wpd1.pausestart1::interval end else '00:00:00'::interval end as pdt1, - case when wpd1.pausestart2 is not null and wpd1.pauseend2 is not null then case when wpd1.pauseend2 < wpd1.pausestart2 then '24:00:00'::interval + wpd1.pauseend2::interval -wpd1. pausestart2::interval else wpd1.pauseend2::interval - wpd1.pausestart2::interval end else '00:00:00'::interval end as pdt2, - case when wpd1.start2 is not null and wpd1.end1 is not null and wpd1.start1 is not null and wpd1.end2 is not null then wpd1.start2::interval - wpd1.end1::interval else null end as interruption + case when wpd1.pausestart1 between wpd1.start1 and case when wpd1.end1 < wpd1.start1 then wpd1.end1::interval + '24:00:00'::interval else wpd1.end1::interval end and wpd1.pausestart1 between wpd1.start1 and case when wpd1.end1 < wpd1.start1 then wpd1.end1::interval + '24:00:00'::interval else wpd1.end1::interval end then case when wpd1.pausestart1 < wpd1.pausestart1 then '24:00:00'::interval + wpd1.pausestart1::interval - wpd1.pausestart1::interval else wpd1.pausestart1::interval - wpd1.pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when wpd1.pausestart1 between wpd1.start2 and case when wpd1.end2 < wpd1.start2 then wpd1.end2::interval + '24:00:00'::interval else wpd1.end2::interval end and wpd1.pauseend2 between wpd1.start2 and case when wpd1.end2 < wpd1.start2 then wpd1.end2::interval + '24:00:00'::interval else wpd1.end2::interval end then case when wpd1.pauseend2 < wpd1.pausestart2 then '24:00:00'::interval + wpd1.pauseend2::interval - wpd1.pausestart2::interval else wpd1.pauseend2::interval - wpd1.pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when wpd1.start2 is not null and wpd1.end1 is not null and wpd1.start1 is not null and wpd1.end2 is not null then + wpd1.start2::interval - wpd1.end1::interval + else null end as interruption FROM ".$schema.".workplandays wpd1 LEFT JOIN ".$schema.".workplans wp1 ON wpd1.id_workplan = wp1.id where wpd1.id_workplan='".$id_workplan."') wpd ORDER BY wpd.workplan, wpd.weekday;"; diff --git a/app_plandutravail_lu/lib/version.php b/app_plandutravail_lu/lib/version.php index 7165a0a9..04cad6ea 100644 --- a/app_plandutravail_lu/lib/version.php +++ b/app_plandutravail_lu/lib/version.php @@ -1,5 +1,5 @@ date('2022-06-05') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-06-05')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-06-05'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-06-05') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-06-05')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-06-05'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-06-05') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-06-05')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-06-05'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-06-05') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-06-05')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-07-31') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-07-31')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-07-31') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-07-31')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-07-31') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-07-31')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-07-31') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-07-31')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-10-02') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-10-02')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-10-02') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-10-02')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-10-02') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-10-02')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','27a6cfdb-4660-1b93-ef87-7f1619610561') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); + timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='21',contracthours='84:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +taffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'27a6cfdb-4660-1b93-ef87-7f1619610561' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +taffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='21',contracthours='84:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='21',contracthours='84:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','10f3831e-5651-96f0-2bf7-249c106bce8c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='21',contracthours='84:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=0,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'10f3831e-5651-96f0-2bf7-249c106bce8c' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-10-26'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and (daydate > date('2022-12-04') or daydate < date('2020-10-26')) + OR (id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' AND daydate not between date('2020-10-26') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='10f3831e-5651-96f0-2bf7-249c106bce8c' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=2,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=0,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-03-14'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; + as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=2,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +nd2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=0,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','73f58f5d-6ca3-0d1f-4bc4-510cabb30173') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-03-14'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=2,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=0,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=2,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-03-14'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-03-14'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +rval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +idhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'73f58f5d-6ca3-0d1f-4bc4-510cabb30173' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='73f58f5d-6ca3-0d1f-4bc4-510cabb30173' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','f678699b-2be3-c2a0-067d-2fec6ce45e61') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +p.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='917:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'f678699b-2be3-c2a0-067d-2fec6ce45e61' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; + as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='f678699b-2be3-c2a0-067d-2fec6ce45e61' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='315:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1=null,timeend1=null,timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='917:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='917:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +rs=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','78f6e494-b919-2416-0187-640e454d6846') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='315:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=1,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=2,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +idhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='917:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='315:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='308:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +s.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='315:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='301:00:00' where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'78f6e494-b919-2416-0187-640e454d6846' as id_staff, null as payedpause, + '07:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '07:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='78f6e494-b919-2416-0187-640e454d6846' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='78f6e494-b919-2416-0187-640e454d6846' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='78f6e494-b919-2416-0187-640e454d6846' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='78f6e494-b919-2416-0187-640e454d6846' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +terval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1=null,timeend1=null,timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +terval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','ec24d067-154f-2712-52c9-ed19e5251b20') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +etween rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'ec24d067-154f-2712-52c9-ed19e5251b20' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='ec24d067-154f-2712-52c9-ed19e5251b20' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +urs, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='14:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-08-08'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','a4e63a91-bd60-7343-9ce9-80c6d1600938') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +l and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=3,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); + timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=3,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=4,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='524:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='176:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=6,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=7,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='08:00',timeend1='12:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='180:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +s.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-08-08'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='172:00:00' where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'a4e63a91-bd60-7343-9ce9-80c6d1600938' as id_staff, null as payedpause, + '04:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '04:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='a4e63a91-bd60-7343-9ce9-80c6d1600938' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=6,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=7,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f74b9c10-7409-54fd-b8e8-e6f898d299e0') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-10-01') and date('2022-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: insert into demo15.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0faf88cb-b59c-b0e9-dedf-887834b11e74','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9bba4e8a-e9f3-9287-6023-d53a7282b6db','5f755fbe-d122-8b1e-bc29-0c257866903b') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=6,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f8af1914-dcc8-1482-b978-9c20339eb3aa','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'5f755fbe-d122-8b1e-bc29-0c257866903b' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' group by rp.id); +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('01eedb60-0eda-058e-9e26-4d5b72751963','5f755fbe-d122-8b1e-bc29-0c257866903b') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('2ad15899-0e9c-1942-d758-1d5fc92aa24b','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'5f755fbe-d122-8b1e-bc29-0c257866903b' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942','5f755fbe-d122-8b1e-bc29-0c257866903b') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=7,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('e6ad6258-9c36-8e68-1082-473dfcadfaa9','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'5f755fbe-d122-8b1e-bc29-0c257866903b' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='276d6198-0983-ec8a-febf-ec4ee54fae43'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: delete from demo15.staffreportperiod sp where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('55d323b3-9990-b2a0-c441-315770790f8c','5f755fbe-d122-8b1e-bc29-0c257866903b') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-10-01'))) and date(date_trunc('week',date('2021-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'5f755fbe-d122-8b1e-bc29-0c257866903b' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='5f755fbe-d122-8b1e-bc29-0c257866903b' group by rp.id); +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('276d6198-0983-ec8a-febf-ec4ee54fae43','5f755fbe-d122-8b1e-bc29-0c257866903b') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '276d6198-0983-ec8a-febf-ec4ee54fae43') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-10-01') and date('2021-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: INSERT INTO demo15.staffreportperiod (id_reportperiod, id_staff) + VALUES ('f74b9c10-7409-54fd-b8e8-e6f898d299e0','06966d49-f1b1-fade-c263-0350b688eeaf') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0faf88cb-b59c-b0e9-dedf-887834b11e74'; +EXEC: insert into demo15.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'06966d49-f1b1-fade-c263-0350b688eeaf' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2020-09-28'::timestamp,'2022-12-04'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: DELETE FROM demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and (daydate > date('2022-12-04') or daydate < date('2020-09-28')) + OR (id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' AND daydate not between date('2020-09-28') and date('2022-12-04')); +EXEC: delete from demo15.staffreportperiod sp where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod not in ( +select rp.id from demo15.reportperiod rp left join demo15.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' group by rp.id); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-12-01'))) and date(date_trunc('week',date('2022-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'e6ad6258-9c36-8e68-1082-473dfcadfaa9') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-06-01') and date('2022-07-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='9bba4e8a-e9f3-9287-6023-d53a7282b6db'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0faf88cb-b59c-b0e9-dedf-887834b11e74') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-12-01') and date('2022-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-10-01'))) and date(date_trunc('week',date('2020-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='45',contracthours='360:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='f8af1914-dcc8-1482-b978-9c20339eb3aa'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2020-11-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9bba4e8a-e9f3-9287-6023-d53a7282b6db') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-10-01') and date('2020-11-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-02-01'))) and date(date_trunc('week',date('2022-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperiod set maxdays='44',contracthours='352:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='01eedb60-0eda-058e-9e26-4d5b72751963'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'f8af1914-dcc8-1482-b978-9c20339eb3aa') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-02-01') and date('2022-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2020-12-01'))) and date(date_trunc('week',date('2021-01-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='f74b9c10-7409-54fd-b8e8-e6f898d299e0'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='2ad15899-0e9c-1942-d758-1d5fc92aa24b'; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-10-01'))) and date(date_trunc('week',date('2022-11-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=5,last44date=date('2021-01-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '01eedb60-0eda-058e-9e26-4d5b72751963') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2020-12-01') and date('2021-01-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-04-01'))) and date(date_trunc('week',date('2022-05-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '2ad15899-0e9c-1942-d758-1d5fc92aa24b') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2022-04-01') and date('2022-05-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-02-01'))) and date(date_trunc('week',date('2021-03-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='43',contracthours='344:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='e6ad6258-9c36-8e68-1082-473dfcadfaa9'; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=1,status44hcount=6,last44date=date('2021-03-29'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0c8f8e8a-f19b-e0e0-0477-ecb9b2f7b942') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-02-01') and date('2021-03-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2022-06-01'))) and date(date_trunc('week',date('2022-07-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod set maxdays='131',contracthours='1048:00:00' where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and id_reportperiod='55d323b3-9990-b2a0-c441-315770790f8c'; +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and daydate between date(date_trunc('week',date('2021-04-01'))) and date(date_trunc('week',date('2021-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=7,last44date=date('2021-04-05'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '55d323b3-9990-b2a0-c441-315770790f8c') + where pd.id_staff='06966d49-f1b1-fade-c263-0350b688eeaf' and pd.daydate between date('2021-04-01') and date('2021-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='15:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='08:00',timeend1='13:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='08:00',timeend1='13:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='08:00',timeend1='13:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-08-08'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='14:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='08:00',timeend1='14:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='3241c58c-3733-d93b-4acb-dbad468ee078'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-02') and id not in ('efb65c65-7f0b-b740-b280-ae138978af87'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='16:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='15:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-08-08'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE demo15.staffreportperioddays SET timestart1='10:00',timeend1='15:00',timestart2=null,timeend2=null,pausestart1=null,pauseend1=null,pausestart2=null,pauseend2=null,payedpause=null,timepause='00:00',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='de536f11-6513-6d09-9b68-8e3ffc387c30'; +EXEC: DELETE FROM demo15.timetracker WHERE id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate=date('2022-08-01') and id not in ('42870479-28e7-e952-2a96-301d0035c9bb'); +EXEC: update demo15.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from demo15.staffreportperioddays where id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update demo15.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from demo15.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from demo15.staffreportperioddays where id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and daydate between date(date_trunc('week',date('2022-08-01'))) and date(date_trunc('week',date('2022-09-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update demo15.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2022-08-08'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from demo15.staffreportperioddays pd + join demo15.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '0ed3eb53-bc4f-9d95-e90c-0b20dbd38e44') + where pd.id_staff='27a6cfdb-4660-1b93-ef87-7f1619610561' and pd.daydate between date('2022-08-01') and date('2022-09-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from public.sessions where id_user='e9e6880b-75c7-70b5-ce61-49cf6d62630f' and remote_addr='::1' and user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'; +EXEC: update public.sessions set sessiondata='{"schemata":"kraeltgen"}' where id='0f0f9696-a244-2b4c-e0d9-9051e3909571' +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('a0538335-1f82-8312-8a62-2e42ea74ed20','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('ec6261df-f5df-d1b9-7ca1-639d79e82bfe','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('d1fe1b59-0129-bb85-53da-a4f9f69d9b83','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('35fda5d9-36ef-528b-4339-286589719039','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('b8cf8db4-da76-6cd2-7f7e-c44dd40fedae','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('3aabb859-2b24-c8eb-f362-ea11fc60e1e4','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('c6ecc3c5-6c48-659b-84f3-c671b0d78d5e','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('951dde5e-a2b0-932a-e5a3-02502245ed39','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: INSERT INTO kraeltgen.staffreportperiod (id_reportperiod, id_staff) + VALUES ('9d8d0448-32ac-7e59-daef-c0a3a1336150','e3ed2959-a411-2f14-b40a-27251d8f6d0c') on conflict on constraint uniq_staffreportperiod_cal do nothing; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2021-08-09'::timestamp,'2021-08-26'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2021-10-03'::timestamp,'2022-07-10'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '08:00:00' as wdcontracthours +FROM generate_series( '2022-07-11'::timestamp,'2022-07-31'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '08:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-08-01'::timestamp,'2022-10-02'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: insert into kraeltgen.staffreportperioddays (daydate,id_staff,payedpause,wdcontracthours) +SELECT date_trunc('day', dd)::date as daydate,'e3ed2959-a411-2f14-b40a-27251d8f6d0c' as id_staff, null as payedpause, + '05:00:00' as wdcontracthours +FROM generate_series( '2022-10-03'::timestamp,'2023-10-08'::timestamp,'1 day'::interval) dd on conflict on constraint uniq_staffworplan_staffday +do update set wdcontracthours = '05:00:00'; +EXEC: DELETE FROM kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and (daydate > date('2023-10-08') or daydate < date('2021-08-09')) + OR (id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' AND daydate not between date('2021-08-09') and date('2021-08-26') AND daydate not between date('2021-10-03') and date('2022-07-10') AND daydate not between date('2022-07-11') and date('2022-07-31') AND daydate not between date('2022-08-01') and date('2022-10-02') AND daydate not between date('2022-10-03') and date('2023-10-08')); +EXEC: delete from kraeltgen.staffreportperiod sp where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod not in ( +select rp.id from kraeltgen.reportperiod rp left join kraeltgen.staffreportperioddays pd on (pd.daydate between rp.startdate and rp.enddate) +where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by rp.id); +EXEC: update kraeltgen.staffreportperiod set maxdays='10',contracthours='80:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='a0538335-1f82-8312-8a62-2e42ea74ed20'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-07-01'))) and date(date_trunc('week',date('2021-08-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-07-01'))) and date(date_trunc('week',date('2021-08-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=1,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=1,last44date=date('2021-08-30'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'a0538335-1f82-8312-8a62-2e42ea74ed20') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2021-07-01') and date('2021-08-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='5',contracthours='25:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='ec6261df-f5df-d1b9-7ca1-639d79e82bfe'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-09-01'))) and date(date_trunc('week',date('2021-10-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-09-01'))) and date(date_trunc('week',date('2021-10-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=6,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=2,last44date=date('2021-10-25'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'ec6261df-f5df-d1b9-7ca1-639d79e82bfe') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2021-09-01') and date('2021-10-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='8',contracthours='40:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='d1fe1b59-0129-bb85-53da-a4f9f69d9b83'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-11-01'))) and date(date_trunc('week',date('2021-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2021-11-01'))) and date(date_trunc('week',date('2021-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=13,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=4,last44date=date('2021-12-27'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'd1fe1b59-0129-bb85-53da-a4f9f69d9b83') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2021-11-01') and date('2021-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='26',contracthours='130:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='35fda5d9-36ef-528b-4339-286589719039'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2022-01-01'))) and date(date_trunc('week',date('2022-06-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2022-01-01'))) and date(date_trunc('week',date('2022-06-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=34,suppvacancysunwork=2,suppvacancy44hours=1,status44hcount=3,last44date=date('2022-06-27'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '35fda5d9-36ef-528b-4339-286589719039') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2022-01-01') and date('2022-06-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='38',contracthours='235:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='b8cf8db4-da76-6cd2-7f7e-c44dd40fedae'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=43,suppvacancysunwork=2,suppvacancy44hours=2,status44hcount=6,last44date=date('2022-12-26'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'b8cf8db4-da76-6cd2-7f7e-c44dd40fedae') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2022-07-01') and date('2022-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='26',contracthours='130:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='3aabb859-2b24-c8eb-f362-ea11fc60e1e4'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2023-01-01'))) and date(date_trunc('week',date('2023-06-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2023-01-01'))) and date(date_trunc('week',date('2023-06-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=7,last44date=date('2023-01-02'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '3aabb859-2b24-c8eb-f362-ea11fc60e1e4') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2023-01-01') and date('2023-06-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='14',contracthours='70:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='c6ecc3c5-6c48-659b-84f3-c671b0d78d5e'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2023-07-01'))) and date(date_trunc('week',date('2023-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2023-07-01'))) and date(date_trunc('week',date('2023-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'c6ecc3c5-6c48-659b-84f3-c671b0d78d5e') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2023-07-01') and date('2023-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='0',contracthours='00:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='951dde5e-a2b0-932a-e5a3-02502245ed39'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2024-01-01'))) and date(date_trunc('week',date('2024-06-30')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2024-01-01'))) and date(date_trunc('week',date('2024-06-30')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '951dde5e-a2b0-932a-e5a3-02502245ed39') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2024-01-01') and date('2024-06-30') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: update kraeltgen.staffreportperiod set maxdays='0',contracthours='00:00:00' where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and id_reportperiod='9d8d0448-32ac-7e59-daef-c0a3a1336150'; +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and timeend1 and pauseend1 between timestart1 and timeend1 then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and timeend2 and pauseend2 between timestart2 and timeend1 then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2024-07-01'))) and date(date_trunc('week',date('2024-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and daydate between date(date_trunc('week',date('2024-07-01'))) and date(date_trunc('week',date('2024-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=null,suppvacancysunwork=null,suppvacancy44hours=null,status44hcount=null,last44date=null,avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = '9d8d0448-32ac-7e59-daef-c0a3a1336150') + where pd.id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' and pd.daydate between date('2024-07-01') and date('2024-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: insert into kraeltgen.staffvacancyyear (id_staff,vacyear) + select id_staff,to_char(daydate,'YYYY')::int4 as vyear from kraeltgen.staffreportperioddays where id_staff='e3ed2959-a411-2f14-b40a-27251d8f6d0c' group by id_staff,vyear order by id_staff,vyear + on conflict on constraint staffvacancyyear_id_staff_vacyear_key do nothing; +EXEC: delete from public.sessions where id_user='e9e6880b-75c7-70b5-ce61-49cf6d62630f' and remote_addr='::1' and user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'; +EXEC: update public.sessions set sessiondata='{"schemata":"davincenzo"}' where id='2d9ce32a-1604-664f-e6bb-a0d1c175ffce' +EXEC: update public.sessions set sessiondata='{"schemata":"kraeltgen"}' where id='2d9ce32a-1604-664f-e6bb-a0d1c175ffce' +EXEC: UPDATE kraeltgen.staffreportperioddays SET timestart1='18:00',timeend1='00:15',timestart2=null,timeend2=null,pausestart1='22:00',pauseend1='22:30',pausestart2=null,pauseend2=null,payedpause=null,timepause='00:30',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='c9b126e5-394b-2aa1-512b-a1c8ece8e00c'; +EXEC: DELETE FROM kraeltgen.timetracker WHERE id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate=date('2022-07-06') and id not in (''); +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end and pauseend1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end and pauseend2 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=49,suppvacancysunwork=2,suppvacancy44hours=1,status44hcount=4,last44date=date('2022-12-26'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'b8cf8db4-da76-6cd2-7f7e-c44dd40fedae') + where pd.id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and pd.daydate between date('2022-07-01') and date('2022-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE kraeltgen.staffreportperioddays SET timestart1='18:00',timeend1='00:30',timestart2=null,timeend2=null,pausestart1='22:00',pauseend1='22:30',pausestart2=null,pauseend2=null,payedpause=null,timepause='00:30',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='80cf2666-ce43-b67d-8cf9-d408d1fb4a45'; +EXEC: DELETE FROM kraeltgen.timetracker WHERE id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate=date('2022-07-07') and id not in (''); +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end and pauseend1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end and pauseend2 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=49,suppvacancysunwork=2,suppvacancy44hours=1,status44hcount=4,last44date=date('2022-12-26'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'b8cf8db4-da76-6cd2-7f7e-c44dd40fedae') + where pd.id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and pd.daydate between date('2022-07-01') and date('2022-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE kraeltgen.staffreportperioddays SET timestart1='18:00',timeend1='01:00',timestart2=null,timeend2=null,pausestart1='18:00',pauseend1='18:30',pausestart2=null,pauseend2=null,payedpause=null,timepause='00:30',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='c125efcc-0da9-020a-bceb-d17c26129970'; +EXEC: DELETE FROM kraeltgen.timetracker WHERE id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate=date('2022-07-08') and id not in (''); +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end and pauseend1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end and pauseend2 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=49,suppvacancysunwork=2,suppvacancy44hours=1,status44hcount=4,last44date=date('2022-12-26'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'b8cf8db4-da76-6cd2-7f7e-c44dd40fedae') + where pd.id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and pd.daydate between date('2022-07-01') and date('2022-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: UPDATE kraeltgen.staffreportperioddays SET timestart1='18:00',timeend1='02:00',timestart2=null,timeend2=null,pausestart1='22:00',pauseend1='22:30',pausestart2=null,pauseend2=null,payedpause=null,timepause='00:30',daycomment=null,vacancycomment=null,vacancyhours=null,vacillcomment=null,vacillhours=null,vacextracomment=null,vacextrahours=null,recupcomment=null,recuperationhours=null,partunempcomment=null,partunemplhours=null,otherpaidhourscomment=null,otherpaidhours=null WHERE id='9bcb58e4-dd4c-4029-846c-e10e294a9786'; +EXEC: DELETE FROM kraeltgen.timetracker WHERE id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate=date('2022-07-09') and id not in (''); +EXEC: update kraeltgen.staffreportperioddays spds set + workhours=ds.worktime, timepause=case when ds.timepause::interval > '00:00:00' then ds.timepause else null end, + dayhours=(ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours )::interval, interruptionhours = ds.interruption , + contracthours=case when ds.worktime::interval + ds.vachours::interval + ds.vacillhours::interval + ds.vacextrahours::interval + ds.otherpaidhours::interval + ds.recup::interval + ds.partunemplhours::interval > '00:00:00'::interval then ds.wdcontracthours else null end + from ( + select preds.id,case when preds.recuperationhours is null then '00:00:00'::interval else preds.recuperationhours::interval end as recup, + case when preds.vacancyhours is null then '00:00:00'::time else preds.vacancyhours end as vachours, + case when preds.vacillhours is null then '00:00:00'::time else preds.vacillhours end as vacillhours, + case when preds.vacextrahours is null then '00:00:00'::time else preds.vacextrahours end as vacextrahours, + case when preds.otherpaidhours is null then '00:00:00'::time else preds.otherpaidhours end as otherpaidhours, + case when preds.partunemplhours is null then '00:00:00'::time else preds.partunemplhours end as partunemplhours, + preds.interruption, + (preds.dt1+preds.dt2)-(preds.pdt1+preds.pdt2) as worktime, + (preds.pdt1+preds.pdt2) as timepause, + wdcontracthours + from ( select id, recuperationhours,vacancyhours,vacillhours,vacextrahours,otherpaidhours,payedpause,partunemplhours,nighthours, + case when timestart1 is not null and timeend1 is not null then case when timeend1 < timestart1 then '24:00:00'::interval + timeend1::interval - timestart1::interval else timeend1::interval - timestart1::interval end else '00:00:00'::interval end as dt1, + case when timestart2 is not null and timeend2 is not null then case when timeend2 < timestart2 then '24:00:00'::interval + timeend2::interval - timestart2::interval else timeend2::interval - timestart2::interval end else '00:00:00'::interval end as dt2, + case when pausestart1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end and pauseend1 between timestart1 and case when timeend1 < timestart1 then timeend1::interval + '24:00:00'::interval else timeend1::interval end then case when pauseend1 < pausestart1 then '24:00:00'::interval + pauseend1::interval - pausestart1::interval else pauseend1::interval - pausestart1::interval end else '00:00:00'::interval end as pdt1, + case when pausestart1 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end and pauseend2 between timestart2 and case when timeend2 < timestart2 then timeend2::interval + '24:00:00'::interval else timeend2::interval end then case when pauseend2 < pausestart2 then '24:00:00'::interval + pauseend2::interval - pausestart2::interval else pauseend2::interval - pausestart2::interval end else '00:00:00'::interval end as pdt2, + case when timestart2 is not null and timeend1 is not null and timestart1 is not null and timeend2 is not null then + timestart2::interval - timeend1::interval + else null end as interruption, + wdcontracthours + from kraeltgen.staffreportperioddays where id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) preds) ds where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperioddays spds set + nighthours=nh from ( + select id,daydate,timestart1,timeend1, timestart2,timeend2, + coalesce((case when timeend1 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend1::interval end) - + (case when timestart1 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart1::interval end),'00:00:00'::interval) + + coalesce((case when timeend2 not between '01:00:01'::interval and '05:59:59'::interval then '06:00:00'::interval else timeend2::interval end) - + (case when timestart2 not between '01:00:01'::interval and '05:59:59'::interval then '01:00:00'::interval else timestart2::interval end),'00:00:00'::interval) as nh + from kraeltgen.staffreportperioddays s where + (timeend1 between '01:00:01' and '05:59:59' or timeend2 between '01:00:01' and '05:59:59' + or timestart1 between '01:00:01' and '05:59:59' or timestart2 between '01:00:01' and '05:59:59') + and id in (select id from kraeltgen.staffreportperioddays where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and daydate between date(date_trunc('week',date('2022-07-01'))) and date(date_trunc('week',date('2022-12-31')) + interval '6 days')) + ) ds + where spds.id=ds.id; +EXEC: update kraeltgen.staffreportperiod rpd set + --contracthours=csp.contracthours, + workhours=csp.workhours, + vacancyill=csp.vacillhours, + vacancynormal=csp.vacancynormal, + vacancyextra=csp.vacextrahours, + otherpaidhours=csp.otherpaidhours, + partunemplhours=csp.partunemplhours, + totalhours=csp.totalhours, + nighthours=csp.nighthours, + recuperationhours=csp.recuperationhours,statussuncount=49,suppvacancysunwork=2,suppvacancy44hours=1,status44hcount=4,last44date=date('2022-12-26'),avgtotalweekhours=null,hoursdiff=csp.diffhours + FROM ( + select contracthours,workhours,totalhours,recuperationhours, totalhours-contracthours as diffhours,id,vacillhours,vacancynormal,vacextrahours,otherpaidhours,partunemplhours,nighthours from ( + select dp.id, + sum(pd.workhours) as workhours, + sum(pd.dayhours) as totalhours, + sum(coalesce(pd.nighthours,'00:00:00'::interval)) as nighthours, + sum(coalesce(pd.vacancyhours,'00:00:00'::time)) as vacancynormal, + sum(coalesce(pd.vacillhours,'00:00:00'::time)) as vacillhours, + sum(coalesce(pd.vacextrahours,'00:00:00'::time)) as vacextrahours, + sum(coalesce(pd.otherpaidhours,'00:00:00'::time)) as otherpaidhours, + sum(coalesce(pd.partunemplhours,'00:00:00'::time)) as partunemplhours, + sum(coalesce(pd.recuperationhours,'00:00:00'::time)) as recuperationhours, + coalesce(dp.contracthours,'00:00:00'::interval) as contracthours, + coalesce(dp.payedhours0,'00:00:00'::interval) as payedhours0, + coalesce(dp.payedhours40,'00:00:00'::interval) as payedhours40, + coalesce(dp.transferedhours,'00:00:00'::interval) as transferedhours + from kraeltgen.staffreportperioddays pd + join kraeltgen.staffreportperiod dp on (pd.id_staff=dp.id_staff and dp.id_reportperiod = 'b8cf8db4-da76-6cd2-7f7e-c44dd40fedae') + where pd.id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and pd.daydate between date('2022-07-01') and date('2022-12-31') + group by dp.id,dp.payedhours,dp.transferedhourscalc + ) rp) csp where csp.id=rpd.id; +EXEC: delete from public.sessions where id_user='e9e6880b-75c7-70b5-ce61-49cf6d62630f' and remote_addr='::1' and user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'; +EXEC: UPDATE demo.workplandays SET start1='10:00', end1='14:00', start2='18:00', end2='22:00', pause=null, pausestart1='14:00', pauseend1='14:30', pausestart2='17:30', pauseend2='18:00' WHERE id='c7957c42-81f9-96f8-0f5d-ad7515503674'; +EXEC: UPDATE demo.workplans SET weekhours='40:30:00', weekdays=6, freeweekdayslist='["7"]' WHERE id='fbd5ac24-becf-c358-45c4-65518415ebb8' +EXEC: update demo.workplandays set start1='10:00:00',end1='14:00:00',start2='18:00:00',end2='22:00:00', pause=null,pausestart1='14:00:00',pauseend1='14:30:00',pausestart2='17:30:00',pauseend2='18:00:00' where id='d33cbba0-9602-54e1-98b8-401ab199dc5a'; +EXEC: update demo.workplandays set start1='10:00:00',end1='14:00:00',start2='18:00:00',end2='22:00:00', pause=null,pausestart1='14:00:00',pauseend1='14:30:00',pausestart2='17:30:00',pauseend2='18:00:00' where id='f39ad346-e714-7876-5689-2994e1312b36'; +EXEC: update demo.workplandays set start1='10:00:00',end1='14:00:00',start2='18:00:00',end2='22:00:00', pause=null,pausestart1='14:00:00',pauseend1='14:30:00',pausestart2='17:30:00',pauseend2='18:00:00' where id='c59edca6-4e4f-39a9-0427-5a7aeaec58cc'; +EXEC: update demo.workplandays set start1='10:00:00',end1='14:00:00',start2='18:00:00',end2='22:00:00', pause=null,pausestart1='14:00:00',pauseend1='14:30:00',pausestart2='17:30:00',pauseend2='18:00:00' where id='2616bad1-9854-3fce-885b-5ceaa63d0f15'; +EXEC: UPDATE demo.workplandays SET start1=null, end1=null, start2=null, end2=null, pause=null, pausestart1=null, pauseend1=null, pausestart2=null, pauseend2=null WHERE id='3436f93a-c588-4d5d-0c91-7df00c36695a'; +EXEC: UPDATE demo.workplans SET weekhours='40:00:00', weekdays=5, freeweekdayslist='["7","6"]' WHERE id='fbd5ac24-becf-c358-45c4-65518415ebb8' +EXEC: UPDATE demo.workplandays SET start1='10:00', end1='14:30', start2='18:00', end2='22:00', pause='00:30', wpd1.pausestart1=null, pauseend1=null, pausestart2='17:30', pauseend2='18:00' WHERE id='c7957c42-81f9-96f8-0f5d-ad7515503674'; +EXEC: UPDATE demo.workplans SET weekhours='40:00:00', weekdays=5, freeweekdayslist='["7","6"]' WHERE id='fbd5ac24-becf-c358-45c4-65518415ebb8' +EXEC: UPDATE demo.workplandays SET start1='10:00', end1='14:30', start2='18:00', end2='22:00', pause='00:30', pausestart1='14:00', pauseend1='14:30', pausestart2='17:30', pauseend2='18:00' WHERE id='c7957c42-81f9-96f8-0f5d-ad7515503674'; +EXEC: UPDATE demo.workplans SET weekhours='40:00:00', weekdays=5, freeweekdayslist='["7","6"]' WHERE id='fbd5ac24-becf-c358-45c4-65518415ebb8' +EXEC: delete from public.sessions where id_user='e9e6880b-75c7-70b5-ce61-49cf6d62630f' and remote_addr='192.168.178.50' and user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackertype=null WHERE id='26be2229-3d60-a1df-457d-791bc10ca104'; +EXEC: UPDATE public.companies SET company='Restaurant Demo' WHERE id='26be2229-3d60-a1df-457d-791bc10ca104'; +EXEC: UPDATE public.companies SET comregister=null WHERE id='26be2229-3d60-a1df-457d-791bc10ca104'; +EXEC: UPDATE public.companies SET company='Restaurant Demo' WHERE id='26be2229-3d60-a1df-457d-791bc10ca104'; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: delete from public.sessions where id_user='e9e6880b-75c7-70b5-ce61-49cf6d62630f' and remote_addr='::1' and user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET vatnumber=null WHERE id='e028cdac-fb5d-5c68-b39f-a8876422a61e'; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackeruser='ksaffran@dks.lu' WHERE id=''; +EXEC: UPDATE public.companies SET timetrackerpassword='ntHesDUDWeQVs' WHERE id=''; diff --git a/app_plandutravail_lu/log/sqlx.log b/app_plandutravail_lu/log/sqlx.log index e69de29b..b6813128 100644 --- a/app_plandutravail_lu/log/sqlx.log +++ b/app_plandutravail_lu/log/sqlx.log @@ -0,0 +1,276 @@ +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-06') between startdate and enddate) or (startdate <= date('2022-07-06') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-06') between startdate and enddate) or (startdate <= date('2022-07-06') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-03' as datebefore, + '23:00' as timebefore, + '2022-07-07' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-07') between startdate and enddate) or (startdate <= date('2022-07-07') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-06' as datebefore, + '00:15' as timebefore, + '2022-07-08' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-01') between startdate and enddate) or (startdate <= date('2022-07-01') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-06-26' as datebefore, + '16:00' as timebefore, + '2022-07-02' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-02') between startdate and enddate) or (startdate <= date('2022-07-02') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-01' as datebefore, + '01:00' as timebefore, + '2022-07-03' as dateafter, + '12:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-03') between startdate and enddate) or (startdate <= date('2022-07-03') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-02' as datebefore, + '00:00' as timebefore, + '2022-07-06' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-06') between startdate and enddate) or (startdate <= date('2022-07-06') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-03' as datebefore, + '23:00' as timebefore, + '2022-07-07' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-06') between startdate and enddate) or (startdate <= date('2022-07-06') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-03' as datebefore, + '23:00' as timebefore, + '2022-07-07' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-07') between startdate and enddate) or (startdate <= date('2022-07-07') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-06' as datebefore, + '00:15' as timebefore, + '2022-07-08' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-08') between startdate and enddate) or (startdate <= date('2022-07-08') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-07' as datebefore, + '00:30' as timebefore, + '2022-07-09' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-09') between startdate and enddate) or (startdate <= date('2022-07-09') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-08' as datebefore, + '01:00' as timebefore, + '2022-07-13' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-13') between startdate and enddate) or (startdate <= date('2022-07-13') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-09' as datebefore, + '02:00' as timebefore, + '2022-07-14' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-07-15') between startdate and enddate) or (startdate <= date('2022-07-15') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-07-14' as datebefore, + '00:15' as timebefore, + '2022-07-16' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 +DayLimits 1: select weekhours from kraeltgen.staffcontract where id_staff='afe52b22-342a-204a-f5c0-492ac2b23319' and ((date('2022-08-05') between startdate and enddate) or (startdate <= date('2022-08-05') and enddate is null)) order by startdate,enddate nulls first limit 1; +DayLimits <40: select to_char(case when maxdayhours > '30:00:00'::interval then '30:00:00'::interval else maxdayhours end ,'HH24:MI') as maxdayhours, + to_char('30:00:00'::interval * (1+maxhourspercents),'HH24:MI') as maxweekhours, + to_char(wk.maxinterruptionhours,'HH24:MI') as maxinterruptionhours, + to_char(wk.mindailyrecuperation,'HH24:MI') as mindailyrecuperation, + to_char('30:00:00'::interval,'HH24:MI') as defaultweekhours, + '2022-08-04' as datebefore, + '00:00' as timebefore, + '2022-08-06' as dateafter, + '18:00' as timeafter + from kraeltgen.worktimes wk where maxhourspercents is not null and maxdayhours <= '30:00:00' LIMIT 1 diff --git a/app_plandutravail_lu/pwa/manifest.webmanifest b/app_plandutravail_lu/pwa/manifest.webmanifest index 91d7997f..5d926860 100644 --- a/app_plandutravail_lu/pwa/manifest.webmanifest +++ b/app_plandutravail_lu/pwa/manifest.webmanifest @@ -1,6 +1,6 @@ { "name": "POT", - "start_url": "http://dks-laptop/potapp/", + "start_url": "http://dks-laptop/app_plandutravail_lu/", "display": "standalone", "dir": "ltr", "lang": "fr-FR", @@ -9,12 +9,12 @@ "description": "Plan d'Organisation du Travail", "icons": [ { - "src": "http://dks-laptop/potapp/pwa/android-icon-192x192.png", + "src": "http://dks-laptop/app_plandutravail_lu/pwa/android-icon-192x192.png", "sizes": "192x192", "type": "image/png" }, { - "src": "http://dks-laptop/potapp/pwa/poticon512.png", + "src": "http://dks-laptop/app_plandutravail_lu/pwa/poticon512.png", "sizes": "512x512", "type": "image/png" } diff --git a/app_plandutravail_lu/pwa/serviceworker.js b/app_plandutravail_lu/pwa/serviceworker.js index ce6d2d5d..90aff62b 100644 --- a/app_plandutravail_lu/pwa/serviceworker.js +++ b/app_plandutravail_lu/pwa/serviceworker.js @@ -38,7 +38,7 @@ self.addEventListener('message', function (event) { // IIFE is used for lexical scoping instead of just a braces block due to bug in Safari. (() => { - wp.serviceWorker.precaching.precache([{"url":"http:\/\/dks-laptop\/potapp\/login.html","revision":"1.0.0;POT=1.0"},{"url":"http:\/\/dks-laptop\/potapp\/login.html","revision":"1.0.0;POT=1.0"}]); + wp.serviceWorker.precaching.precache([{"url":"http:\/\/dks-laptop\/app_plandutravail_lu\/login.html","revision":"1.0.0;POT=1.0"},{"url":"http:\/\/dks-laptop\/app_plandutravail_lu\/login.html","revision":"1.0.0;POT=1.0"}]); // @todo Should not these parameters be specific to each entry as opposed to all entries? // @todo Should not the strategy be tied to each entry as well? @@ -73,7 +73,7 @@ self.addEventListener('message', function (event) { return caches .match( wp.serviceWorker.precaching.getCacheKeyForURL( - "http:\/\/dks-laptop\/potapp\/login.html" + "http:\/\/dks-laptop\/app_plandutravail_lu\/login.html" ) ) .then(function (errorResponse) { @@ -166,7 +166,7 @@ self.addEventListener('message', function (event) { return caches .match( wp.serviceWorker.precaching.getCacheKeyForURL( - "http:\/\/dks-laptop\/potapp\/login.html" + "http:\/\/dks-laptop\/app_plandutravail_lu\/login.html" ) ) .then(function (response) { @@ -323,7 +323,7 @@ self.addEventListener('message', function (event) { return caches .match( wp.serviceWorker.precaching.getCacheKeyForURL( - "http:\/\/dks-laptop\/potapp\/login.html" + "http:\/\/dks-laptop\/app_plandutravail_lu\/login.html" ) ) .then(function (response) { diff --git a/app_plandutravail_lu/tmpl/block/common_scripts.html b/app_plandutravail_lu/tmpl/block/common_scripts.html index 59ead397..cc83c6a3 100644 --- a/app_plandutravail_lu/tmpl/block/common_scripts.html +++ b/app_plandutravail_lu/tmpl/block/common_scripts.html @@ -3,7 +3,7 @@ // window.addEventListener( 'load', function() { // { // navigator.serviceWorker.register( - // "http:\/\/dks-laptop\/potapp\/pwa\/serviceworker.js", + // "http:\/\/dks-laptop\/app_plandutravail_lu\/pwa\/serviceworker.js", // {"scope":"\/"}).then( reg => {} ); // } // } ); diff --git a/app_plandutravail_lu/tmpl/block/dlgaddperiod.html b/app_plandutravail_lu/tmpl/block/dlgaddperiod.html index 3406864b..a6b13a32 100644 --- a/app_plandutravail_lu/tmpl/block/dlgaddperiod.html +++ b/app_plandutravail_lu/tmpl/block/dlgaddperiod.html @@ -1,5 +1,5 @@ -