├── .editorconfig ├── .gitignore ├── .vscode ├── .ropeproject │ └── config.py └── beancount_share.code-workspace ├── CODEOWNERS ├── LICENSE ├── Makefile ├── README.md ├── beancount_share ├── __init__.py ├── share.py └── utils.py ├── pyproject.toml ├── requirements.txt ├── setup.py └── tests ├── conftest.py ├── context.py ├── features ├── basics.feature ├── configuration.feature ├── currencies.feature ├── errors.feature ├── many-to-many.feature ├── one-to-many.feature ├── one-to-one.feature ├── shortcuts.feature └── usecases.feature └── test_all.py /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | charset = utf-8 9 | end_of_line = lf 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | # 4 space indentation 14 | [*.py] 15 | indent_size = 4 16 | indent_style = space 17 | 18 | # Tab indentation (no size specified) 19 | [Makefile] 20 | indent_style = tab 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | build/ 3 | dist/ 4 | *.egg-info/ 5 | MANIFEST 6 | *.pyc 7 | cov.xml 8 | .coverage 9 | 10 | .venv/ 11 | -------------------------------------------------------------------------------- /.vscode/.ropeproject/config.py: -------------------------------------------------------------------------------- 1 | # The default ``config.py`` 2 | # flake8: noqa 3 | 4 | 5 | def set_prefs(prefs): 6 | """This function is called before opening the project""" 7 | 8 | # Specify which files and folders to ignore in the project. 9 | # Changes to ignored resources are not added to the history and 10 | # VCSs. Also they are not returned in `Project.get_files()`. 11 | # Note that ``?`` and ``*`` match all characters but slashes. 12 | # '*.pyc': matches 'test.pyc' and 'pkg/test.pyc' 13 | # 'mod*.pyc': matches 'test/mod1.pyc' but not 'mod/1.pyc' 14 | # '.svn': matches 'pkg/.svn' and all of its children 15 | # 'build/*.o': matches 'build/lib.o' but not 'build/sub/lib.o' 16 | # 'build//*.o': matches 'build/lib.o' and 'build/sub/lib.o' 17 | prefs['ignored_resources'] = ['*.pyc', '*~', '.ropeproject', 18 | '.hg', '.svn', '_svn', '.git', '.tox'] 19 | 20 | # Specifies which files should be considered python files. It is 21 | # useful when you have scripts inside your project. Only files 22 | # ending with ``.py`` are considered to be python files by 23 | # default. 24 | #prefs['python_files'] = ['*.py'] 25 | 26 | # Custom source folders: By default rope searches the project 27 | # for finding source folders (folders that should be searched 28 | # for finding modules). You can add paths to that list. Note 29 | # that rope guesses project source folders correctly most of the 30 | # time; use this if you have any problems. 31 | # The folders should be relative to project root and use '/' for 32 | # separating folders regardless of the platform rope is running on. 33 | # 'src/my_source_folder' for instance. 34 | #prefs.add('source_folders', 'src') 35 | 36 | # You can extend python path for looking up modules 37 | #prefs.add('python_path', '~/python/') 38 | 39 | # Should rope save object information or not. 40 | prefs['save_objectdb'] = True 41 | prefs['compress_objectdb'] = False 42 | 43 | # If `True`, rope analyzes each module when it is being saved. 44 | prefs['automatic_soa'] = True 45 | # The depth of calls to follow in static object analysis 46 | prefs['soa_followed_calls'] = 0 47 | 48 | # If `False` when running modules or unit tests "dynamic object 49 | # analysis" is turned off. This makes them much faster. 50 | prefs['perform_doa'] = True 51 | 52 | # Rope can check the validity of its object DB when running. 53 | prefs['validate_objectdb'] = True 54 | 55 | # How many undos to hold? 56 | prefs['max_history_items'] = 32 57 | 58 | # Shows whether to save history across sessions. 59 | prefs['save_history'] = True 60 | prefs['compress_history'] = False 61 | 62 | # Set the number spaces used for indenting. According to 63 | # :PEP:`8`, it is best to use 4 spaces. Since most of rope's 64 | # unit-tests use 4 spaces it is more reliable, too. 65 | prefs['indent_size'] = 4 66 | 67 | # Builtin and c-extension modules that are allowed to be imported 68 | # and inspected by rope. 69 | prefs['extension_modules'] = [] 70 | 71 | # Add all standard c-extensions to extension_modules list. 72 | prefs['import_dynload_stdmods'] = True 73 | 74 | # If `True` modules with syntax errors are considered to be empty. 75 | # The default value is `False`; When `False` syntax errors raise 76 | # `rope.base.exceptions.ModuleSyntaxError` exception. 77 | prefs['ignore_syntax_errors'] = False 78 | 79 | # If `True`, rope ignores unresolvable imports. Otherwise, they 80 | # appear in the importing namespace. 81 | prefs['ignore_bad_imports'] = False 82 | 83 | # If `True`, rope will insert new module imports as 84 | # `from import ` by default. 85 | prefs['prefer_module_from_imports'] = False 86 | 87 | # If `True`, rope will transform a comma list of imports into 88 | # multiple separate import statements when organizing 89 | # imports. 90 | prefs['split_imports'] = False 91 | 92 | # If `True`, rope will remove all top-level import statements and 93 | # reinsert them at the top of the module when making changes. 94 | prefs['pull_imports_to_top'] = True 95 | 96 | # If `True`, rope will sort imports alphabetically by module name instead of 97 | # alphabetically by import statement, with from imports after normal 98 | # imports. 99 | prefs['sort_imports_alphabetically'] = False 100 | 101 | # Location of implementation of rope.base.oi.type_hinting.interfaces.ITypeHintingFactory 102 | # In general case, you don't have to change this value, unless you're an rope expert. 103 | # Change this value to inject you own implementations of interfaces 104 | # listed in module rope.base.oi.type_hinting.providers.interfaces 105 | # For example, you can add you own providers for Django Models, or disable the search 106 | # type-hinting in a class hierarchy, etc. 107 | prefs['type_hinting_factory'] = 'rope.base.oi.type_hinting.factory.default_type_hinting_factory' 108 | 109 | 110 | def project_opened(project): 111 | """This function is called after opening the project""" 112 | # Do whatever you like here! 113 | -------------------------------------------------------------------------------- /.vscode/beancount_share.code-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "extensions": { 3 | "recommendations": [ 4 | "alexkrechik.cucumberautocomplete", 5 | "eamodio.gitlens", 6 | "editorconfig.editorconfig", 7 | "jasonnutter.vscode-codeowners", 8 | "ms-python.python", 9 | "ryanluker.vscode-coverage-gutters", 10 | "shardulm94.trailing-spaces", 11 | "tyriar.sort-lines", 12 | "yzhang.markdown-all-in-one" 13 | ] 14 | }, 15 | "folders": [ 16 | { 17 | "path": ".." 18 | } 19 | ], 20 | "settings": { 21 | "coverage-gutters.showLineCoverage": true, 22 | "coverage-gutters.showRulerCoverage": true, 23 | "coverage-gutters.showGutterCoverage": false, 24 | "cucumberautocomplete.steps": [ 25 | "tests/conftest.py" 26 | ], 27 | "cucumberautocomplete.syncfeatures": "tests/*.feature", 28 | "cucumberautocomplete.strictGherkinCompletion": true, 29 | "cucumberautocomplete.strictGherkinValidation": true, 30 | "cucumberautocomplete.smartSnippets": true, 31 | "cucumberautocomplete.stepsInvariants": true, 32 | "cucumberautocomplete.skipDocStringsFormat": false, 33 | "cucumberautocomplete.onTypeFormat": true, 34 | "editor.quickSuggestions": { 35 | "comments": true, 36 | "strings": true, 37 | "other": true 38 | }, 39 | "cucumberautocomplete.gherkinDefinitionPart": "@(given|when|then|step)\\(parsers.parse\\(", 40 | "cucumberautocomplete.customParameters": [ 41 | { 42 | "parameter":"{ab}", 43 | "value":"(a|b)" 44 | }, 45 | { 46 | "parameter":"\\{a.*\\}", 47 | "value":"a" 48 | }, 49 | ], 50 | // "cucumberautocomplete.gherkinDefinitionPart": "@(given|when|then|step)\\((parsers.parse\\()?", // TODO 51 | "files.exclude": { 52 | "**/.git": true, 53 | "**/.svn": true, 54 | "**/.hg": true, 55 | "**/CVS": true, 56 | "**/.DS_Store": true, 57 | "**/__pycache__": true, 58 | "**/.pytest_cache": true, 59 | ".coverage": true, 60 | "cov.xml": true, 61 | "beancount_share.egg-info": true, 62 | "build": true, 63 | "dist": true, 64 | // "beancount_share/": true, 65 | // "tests/": true 66 | }, 67 | "editor.detectIndentation": false, // Let `.editconfig` decide. 68 | "python.linting.pylintEnabled": true, 69 | "python.linting.enabled": true, 70 | "python.formatting.provider": "black", 71 | "python.testing.unittestEnabled": false, 72 | "python.testing.nosetestsEnabled": false, 73 | "python.testing.pytestEnabled": true, 74 | "python.testing.pytestArgs": ["${workspaceFolder}", "--cov-report", "xml:cov.xml", "--cov", "beancount_share", "-v"], 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Ref: 2 | # - https://docs.gitlab.com/ee/user/project/code_owners.html 3 | # - https://github.blog/2017-07-06-introducing-code-owners/ 4 | 5 | * @akuukis 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | install: 2 | python3 -m venv .venv 3 | . .venv/bin/activate; pip3 install -r requirements.txt --upgrade 4 | printf '\nrun:\n source .venv/bin/activate\n\n' 5 | 6 | lint: 7 | black beancount_share/ 8 | 9 | test: 10 | pytest --maxfail=1 -v --cov=beancount_share 11 | 12 | clean: 13 | rm -rf build/* dist/* 14 | 15 | build: clean 16 | python3 setup.py sdist bdist_wheel 17 | 18 | upload: build 19 | twine upload dist/* 20 | 21 | .PHONY: init test 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Share 2 | =============================================================================== 3 | 4 | [![PyPI - Version](https://img.shields.io/pypi/v/beancount_share)](https://pypi.org/project/beancount-share/) 5 | [![PyPI - Downloads](https://img.shields.io/pypi/dm/beancount_share)](https://pypi.org/project/beancount-share/) 6 | [![PyPI - Wheel](https://img.shields.io/pypi/wheel/beancount_share)](https://pypi.org/project/beancount-share/) 7 | [![License](https://img.shields.io/pypi/l/beancount_share)](https://choosealicense.com/licenses/agpl-3.0/) 8 | [![Linting](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) 9 | 10 | A beancount plugin to share expenses with external parties. 11 | 12 | `#share` plugin uses tag syntax to add info to the transaction: 13 | - basic: share expense with another partner 50%-50% - simply use `#share-Bob` 14 | - amount: share a specific sum of expense with another partner - use `#share-Bob-7` 15 | - percentage: share a specific percentage of expense with another partner - use `#share-Bob-40p` 16 | - many: share expense with multiple partners - use `#share-Bob #share-Charlie` 17 | 18 | This plugin is very powerful and most probably can deal with all of your sharing needs. 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Install 28 | =============================================================================== 29 | 30 | ``` 31 | pip3 install beancount_share --user 32 | ``` 33 | 34 | Or copy to path used for python. For example, `$HOME/.local/lib/python3.7/site-packages/beancount_share/*` would do on Debian. If in doubt, look where `beancount` folder is and copy next to it. 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Setup 44 | =============================================================================== 45 | 46 | > Please read the elaborate version at the [Beancount docs](https://docs.google.com/document/d/1MjSpGoJVdgyg8rhKD9otSKo4iSD2VkSYELMWDBbsBiU/edit). 47 | 48 | Add the plugin like this: 49 | 50 | ``` 51 | plugin "beancount_share.share" "{}" 52 | ``` 53 | 54 | Done. If you want to use custom configuration (read below), then you put it inside those `{}` brackets. 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | Usecase: split expense with "Bob" equally. 64 | =============================================================================== 65 | > TL;DR: use `#share-Bob` tag. 66 | 67 | If you, Alice, have had a nice evening out and are in a equal relationship with Bob, you most probably will use the basic share tag that includes only a name: `#share-Bob`. 68 | 69 | The default share tag splits transaction into 2 transactions equally to you and your debtor. 70 | 71 | 72 | 73 | 74 | How to use 75 | ----------------------------------------------------------------------- 76 | 77 | Tag your transaction simply with a tag + name, like `#share-Bob`: 78 | 79 | ``` 80 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 81 | Assets:Cash -10.00 USD 82 | Expenses:Food:Drinks 83 | ``` 84 | 85 | 86 | 87 | 88 | What happens 89 | ----------------------------------------------------------------------- 90 | 91 | The transaction will get transformed into 2 transactions each with 50% of the sum. 92 | The name in the tag will become your debtor (or creditor, if splitting an income). 93 | 94 | ``` 95 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 96 | Assets:Cash -10.00 USD 97 | Expenses:Food:Drinks 5.00 USD 98 | shared: "Assets:Debtors:Bob (50%, 5.00 USD)" 99 | Assets:Debtors:Bob 5.00 USD 100 | shared: "Expenses:Food:Drinks (50%, 5.00 USD)" 101 | ``` 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | Usecase: split expense with "Bob" for a specific amount. 111 | =============================================================================== 112 | > TL;DR: use `#share-Bob-7` tag. 113 | 114 | If you, Alice, have had a nice evening out, and payed also for your friends dinner and he promised to pay you back later, he became your debtor. 115 | You should tag the expense with his name + the sum he owes you: `#share-Bob-7`. 116 | 117 | The amount share tag splits transaction into 2 transactions where your debtors' part is the amount specified and yours - all the rest. 118 | 119 | Note: For Income use `#share-Bob--7`, note the `-7`. 120 | 121 | 122 | How to use 123 | ----------------------------------------------------------------------- 124 | 125 | Tag your transaction with a tag + name + debtors' amount: 126 | 127 | ``` 128 | 2020-01-01 * "BarAlice" "Dinner with friend Bob" #share-Bob-7 129 | Assets:Cash -10.00 USD 130 | Expenses:Food:Drinks 131 | ``` 132 | 133 | 134 | 135 | 136 | What happens 137 | ----------------------------------------------------------------------- 138 | 139 | The transaction will get transformed into 2 transactions. Your debtors' transaction with the specific amount, yours - with all the rest of the sum. 140 | 141 | ``` 142 | 2020-01-01 * "BarAlice" "Dinner with friend Bob" 143 | Assets:Cash -10.00 EUR 144 | Expenses:Food:Drinks 3.00 EUR 145 | shared: "Assets:Debtors:Bob 7.00 EUR" 146 | Assets:Debtors:Bob 7.00 EUR 147 | shared: "Expenses:Food:Drinks 7.00 EUR" 148 | ``` 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | Usecase: split expense with "Bob" for a specific percentage. 158 | =============================================================================== 159 | > TL;DR: use `#share-Bob-40%` tag. 160 | 161 | For example, you, Alice, have had a few drinks with a friend Bob and payed also for his beer. 162 | You both don't remember all the pennies who owns who, but you know that you drank a bit more. 163 | That means you end up with a proportion that he ows you. 164 | You should tag the expense with his name + the percentage of expense he owes you: `#share-Bob-40%`. 165 | 166 | The percentage share tag splits transaction into 2 transactions where your debtors part is the percentage specified and yours - all the rest. 167 | 168 | 169 | 170 | 171 | How to use 172 | ----------------------------------------------------------------------- 173 | 174 | Tag your transaction with a tag + name + debtors' percentage: 175 | 176 | ``` 177 | 2020-01-01 * "BarAlice" "Drinks with friend Bob" #share-Bob-40p 178 | Assets:Cash -10.00 USD 179 | Expenses:Food:Drinks 180 | ``` 181 | 182 | Note: do not forget to add `p` (a **p**ercent, but beancount doesn't allow "%" sign itself), otherwise it will be considered an amount tag! 183 | 184 | 185 | 186 | 187 | What happens 188 | ----------------------------------------------------------------------- 189 | 190 | The transaction will get transformed into 2 transactions. Your debtors' transaction with the sum of specified percentage, yours - with all the rest of the sum. 191 | 192 | ``` 193 | 2020-01-01 * "BarAlice" "Drinks with friend Bob" 194 | Assets:Cash -10.00 EUR 195 | Expenses:Food:Drinks 6.00 EUR 196 | shared: "Assets:Debtors:Bob 40% (4.00 EUR)" 197 | Assets:Debtors:Bob 4.00 EUR 198 | shared: "Expenses:Food:Drinks 40% (4.00 EUR)" 199 | ``` 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | Usecase: split expense with multiple people - "Bob" and "Charlie" - equally. 209 | =============================================================================== 210 | > Tl;DR: use `#share-Bob #share-Charlie` tag. 211 | 212 | If you, Alice, had a few drinks with 2 of your guy friends Bob and Charlie. 213 | You payed for their beer and they became your debtors. 214 | You all like the Mediterrian style of money splitting, so you spilt the evening expenses equally. 215 | You should add 2 tags to the expense, each with a friend's name: `#share-Bob` and `#share-Charlie`. 216 | 217 | 218 | 219 | 220 | How to use 221 | ----------------------------------------------------------------------- 222 | 223 | Tag your transaction with a tag for each person you want to split the transaction with: tag + name: 224 | 225 | ``` 226 | 2020-01-01 * "BarAlice" "Beer with my guy friends" #share-Bob #share-Charlie 227 | Assets:Cash -10.00 USD 228 | Expenses:Food:Drinks 229 | ``` 230 | 231 | 232 | 233 | 234 | What happens 235 | ----------------------------------------------------------------------- 236 | 237 | The transaction will get transformed into as many transactions as tags you have added + your own. 238 | The amount will be spilt equally between all of you. 239 | 240 | ``` 241 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 242 | Assets:Cash -10.00 EUR 243 | Expenses:Food:Drinks 3.34 EUR 244 | shared: "Assets:Debtors:Bob (33%, 3.33 EUR)" 245 | shared901: "Assets:Debtors:Charlie (33%, 3.33 EUR)" 246 | Assets:Debtors:Bob 3.33 EUR 247 | shared: "Expenses:Food:Drinks (33%, 3.33 EUR)" 248 | Assets:Debtors:Charlie 3.33 EUR 249 | shared: "Expenses:Food:Drinks (33%, 3.33 EUR)" 250 | ``` 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | Usecase: something complex. 260 | =============================================================================== 261 | > TL;DR: nope, read on. 262 | 263 | In reality, tags are only the shortcuts of share plugin to make your life easier. 264 | You can always write out the full transaction and sometimes it does make more sense. 265 | 266 | This is an example of a super complex case that might be easier to write out in full syntax: 267 | 268 | You, Alice are a party and had drinks with friends. 269 | You payed for the whole party, but your friends are quite pricky and each has their own specific ways how to count what to repay you. 270 | Bob will give you back an amount of his beer, Charlie is a getleman and wants to pay for the half of all the rest and David just does not care and is ok to spilt in half with you what's left. 271 | 272 | This leaves us with 3 different metas to our transaction: 273 | `Bob-4`, `Charlie-50%`, `David` 274 | 275 | 276 | 277 | 278 | How to use 279 | ----------------------------------------------------------------------- 280 | 281 | Instead of adding tags, you might want to explicitly add meta to the transaction: 282 | 283 | ``` 284 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 285 | Assets:Cash -10.00 EUR 286 | Expenses:Food:Drinks 287 | share: "Bob-4" 288 | share2: "Charlie-50%" 289 | share3: "David" 290 | ``` 291 | 292 | To add many share metas, add a number for each `share` and add amont, percentage or nothing the same as with tags. 293 | 294 | 295 | 296 | 297 | What happens 298 | ----------------------------------------------------------------------- 299 | 300 | The plugin calculates amounts in complex transactions always in the same order: 301 | 302 | The transaction will get transformed into as many transactions as metas you have added + your own. 303 | 304 | The amount will be spilt by these rules in this order: 305 | 1. All absolute amounts are taken away; 306 | 2. The amount that is left now is 100%; 307 | 3. All specified percentages are taken away; 308 | 4. Everything that is left with default metas is split equally; 309 | 310 | ``` 311 | 2020-01-01 * "BarAlice" "Beer with my many friends" 312 | Assets:Cash -10.00 EUR 313 | Expenses:Food:Drinks 1.50 EUR 314 | shared: "Assets:Debtors:Bob 4.00 EUR" 315 | shared901: "Assets:Debtors:Charlie 50% (3.00 EUR)" 316 | shared902: "Assets:Debtors:David (25%, 1.50 EUR)" 317 | Assets:Debtors:Bob 4.00 EUR 318 | shared: "Expenses:Food:Drinks 4.00 EUR" 319 | Assets:Debtors:Charlie 2.40 EUR 320 | shared: "Expenses:Food:Drinks 50% (3.00 EUR)" 321 | Assets:Debtors:David 1.80 EUR 322 | shared: "Expenses:Food:Drinks (25%, 1.50 EUR)" 323 | ``` 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | Other Usage Notes 333 | =============================================================================== 334 | 335 | - Income is shared as well 336 | 337 | 338 | 339 | 340 | Configuration 341 | =============================================================================== 342 | 343 | Note: **Do NOT use double-quotes within the configuration!** The configuration is a Python dict, not a JSON object. 344 | 345 | This is the default configuration in full. Providing it equals to providing no configuration at all. 346 | 347 | ``` 348 | plugin "beancount_share.share" "{ 349 | 'mark_name': 'share', 350 | 'meta_name': 'shared', 351 | 'account_debtors': 'Assets:Debtors', 352 | 'account_creditors': 'Liabilities:Creditors', 353 | 'open_date': '1970-01-01', 354 | 'quantize': '0.01' 355 | }" 356 | ``` 357 | 358 | Note that `meta_name` and `open_date` may also be set to `None` - former to disable informative meta generation, and latter to disable `open` entry creation. Example: 359 | 360 | 361 | ``` 362 | plugin "beancount_share.share" "{ 363 | 'mark_name': 'share', 364 | 'meta_name': None, 365 | 'account_debtors': 'Assets:Debtors', 366 | 'account_creditors': 'Liabilities:Creditors', 367 | 'open_date': None, 368 | 'quantize': '0.01' 369 | }" 370 | ``` 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | Tests 380 | =============================================================================== 381 | 382 | If the examples above do not suffice your needs, check out the tests. 383 | They consist of human-readable examples for more specific cases. 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | Development 393 | =============================================================================== 394 | 395 | Please see Makefile and inline comments. 396 | -------------------------------------------------------------------------------- /beancount_share/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Akuukis/beancount_share/4101c45e16948c0683520cebcf88afe4862224c4/beancount_share/__init__.py -------------------------------------------------------------------------------- /beancount_share/share.py: -------------------------------------------------------------------------------- 1 | """ 2 | Plugin for Beancount to share expenses. 3 | 4 | Credits: Based on Martin Blais personal snippet: https://github.com/beancount/beancount/issues/474. 5 | """ 6 | __author__ = "Akuukis" 7 | 8 | from datetime import date, datetime 9 | from typing import NamedTuple, Set, List, Union, Tuple 10 | from collections import namedtuple 11 | 12 | from beancount.core.inventory import Inventory 13 | from beancount.core.number import D, Decimal, ONE 14 | from beancount.core.amount import Amount 15 | from beancount.core.data import ( 16 | Account, 17 | Entries, 18 | Posting, 19 | Open, 20 | Transaction, 21 | new_metadata, 22 | ) 23 | 24 | from beancount_plugin_utils import metaset, marked 25 | from beancount_plugin_utils.merge_postings import merge_postings 26 | from beancount_plugin_utils.parse_config_string import parse_config_string 27 | from beancount_plugin_utils.BeancountError import plugin_error_handler, posting_error_handler 28 | 29 | from beancount_share.utils import sum_income, sum_expenses 30 | 31 | __plugins__ = ["share"] 32 | 33 | # pylint: disable=raising-non-exception 34 | 35 | 36 | class Config(NamedTuple): 37 | mark_name: str = "share" 38 | meta_name: Union[str, None] = "shared" 39 | account_debtors: str = "Assets:Debtors" 40 | account_creditors: str = "Liabilities:Creditors" 41 | quantize: Decimal = D("0.01") 42 | open_date: Union[date, None] = date.fromisoformat("1970-01-01") 43 | 44 | 45 | PluginShareError = namedtuple("PluginShareError", "source message entry") 46 | 47 | 48 | new_accounts: Set[Account] = set() 49 | 50 | 51 | def share(entries: Entries, unused_options_map, config_string="{}") -> Tuple[Entries, List[NamedTuple]]: 52 | new_entries: Entries = [] 53 | errors: List[NamedTuple] = [] 54 | 55 | # 1. Parse config 56 | with plugin_error_handler(entries, new_entries, errors, "share", PluginShareError): 57 | config = load_config(config_string) 58 | 59 | new_entries[:], errors[:] = marked.on_marked_transactions( 60 | per_marked_transaction, 61 | entries, 62 | config, 63 | config.mark_name, 64 | ("Income", "Expenses"), 65 | PluginShareError, 66 | ) 67 | 68 | if config.open_date != None: 69 | for account in sorted(new_accounts): 70 | new_meta = new_metadata(entries[0].meta["filename"], 0) 71 | open_entry = Open(new_meta, config.open_date, account, None, None) 72 | new_entries.append(open_entry) 73 | 74 | return new_entries, errors 75 | 76 | 77 | def load_config(config_string: str) -> Config: 78 | ############################################################################ 79 | #### Load config (optional) 80 | 81 | # 1. Parse config string. Just copy/paste this block. 82 | config_dict = parse_config_string(config_string) 83 | 84 | # 2. Apply transforms (e.g. from `str` to `date`) where needed. 85 | # Wrap each transform separately with a nice error message. 86 | try: 87 | if "open_date" in config_dict: 88 | config_dict["open_date"] = ( 89 | None if config_dict["open_date"] is None else date.fromisoformat(config_dict["open_date"]) 90 | ) 91 | except: 92 | raise RuntimeError('Bad "open_date" value - it must be a valid date, formatted in UTC (e.g. "2000-01-01").') 93 | 94 | try: 95 | if "quantize" in config_dict: 96 | config_dict["quantize"] = D(config_dict["quantize"]) 97 | except: 98 | raise RuntimeError('Bad "quantize" value - it must be a string that represents a decimal value (e.g. "0.01").') 99 | 100 | # 3. Create config itself. Just copy/paste this block. Done! 101 | return Config(**config_dict) 102 | 103 | 104 | def per_marked_transaction(tx: Transaction, tx_orig: Transaction, config: Config) -> List[Transaction]: 105 | account_prefix: str 106 | total_income = sum_income(tx) 107 | total_expenses = sum_expenses(tx) 108 | total_value: Amount 109 | 110 | if not total_expenses.is_empty() and total_income.is_empty(): 111 | account_prefix = config.account_debtors + ":" 112 | total_value = total_expenses.get_currency_units(tx.postings[0].units.currency) 113 | elif total_expenses.is_empty() and not total_income.is_empty(): 114 | account_prefix = config.account_creditors + ":" 115 | total_value = total_income.get_currency_units(tx.postings[0].units.currency) 116 | else: 117 | raise RuntimeError( 118 | 'Plugin "share" doesn\'t work on transactions that has both income and expense: please split it up into two transactions instead.' 119 | ) 120 | 121 | # 4. Per posting, split it up based on marks. 122 | new_postings = [] 123 | for posting in tx.postings: 124 | with posting_error_handler(tx_orig, posting, PluginShareError): 125 | new_postings.extend(per_marked_posting(posting, config, account_prefix, total_value)) 126 | 127 | for account in new_accounts: 128 | new_postings = merge_postings(account, new_postings, config.meta_name) 129 | 130 | return [tx._replace(postings=new_postings)] 131 | 132 | 133 | def per_marked_posting(posting: Posting, config: Config, account_prefix: str, total_value: Amount): 134 | marks = metaset.get(posting.meta, config.mark_name) 135 | 136 | # 4.1. or skip if not marked. 137 | if len(marks) == 0: 138 | return [posting] 139 | 140 | # 5. Per mark, create a new posting. 141 | todo_absolute: List[Tuple[Amount, str]] = list() 142 | todo_percent: List[Tuple[float, str]] = list() 143 | todo_absent: List[str] = list() 144 | for mark in marks: 145 | parts = [part.replace('$$$', '-') for part in mark.replace('--', '-$$$').split("-")] 146 | account: str 147 | 148 | # 5.1. Apply defaults. 149 | if parts[0] == "": 150 | raise RuntimeError('Plugin "share" requires mark to contain account name, seperated with "-".') 151 | 152 | account = parts[0] if ":" in parts[0] else account_prefix + parts[0] 153 | new_accounts.add(account) 154 | 155 | if len(parts) > 1: 156 | if "%" in parts[1] or "p" in parts[1]: 157 | try: 158 | todo_percent.append( 159 | ( 160 | float(parts[1].split("%")[0].split("p")[0]) / 100, 161 | account, 162 | ) 163 | ) 164 | except Exception: 165 | raise RuntimeError( 166 | 'Something wrong with relative fraction "{}", please use a dot, e.g. "33.33p".'.format(parts[1]) 167 | ) 168 | else: 169 | try: 170 | todo_absolute.append( 171 | ( 172 | Amount( 173 | D(parts[1]).quantize(config.quantize), 174 | posting.units.currency, 175 | ), 176 | account, 177 | ) 178 | ) 179 | except Exception: 180 | raise RuntimeError( 181 | 'Something wrong with absolute fraction "{}", please use a dot, e.g. "2.50".'.format(parts[1]) 182 | ) 183 | else: 184 | todo_absent.append(account) 185 | 186 | total_shared_absolute = sum( 187 | [amount.number for amount, _ in todo_absolute], 188 | D(0).quantize(config.quantize), 189 | ) 190 | total_shared_relative = sum([percent for percent, _ in todo_percent]) 191 | 192 | if total_shared_absolute > abs(total_value.number): 193 | raise RuntimeError("The posting can't share more than it's absolute value") 194 | 195 | if total_shared_relative > 1: 196 | raise RuntimeError( 197 | "The posting can't share more percent than 100%.", 198 | ) 199 | 200 | if total_shared_absolute == abs(total_value.number) and total_shared_relative > 0: 201 | raise RuntimeError("It doesn't make sense to split a remaining amount of zero.") 202 | 203 | if total_shared_relative == 1 and len(todo_absent) > 0: 204 | raise RuntimeError("It doesn't make sense to further auto-split when amount is already split for full 100%.") 205 | 206 | new_postings_inner = [] 207 | # 5.2. Handle absolute amounts first: mutate original posting's amount & create new postings. 208 | for amount, account in todo_absolute: 209 | posting = posting._replace( 210 | units=posting.units._replace(number=(posting.units.number - amount.number).quantize(config.quantize)), 211 | ) 212 | if config.meta_name is not None: 213 | posting = posting._replace( 214 | meta=metaset.add(posting.meta, config.meta_name, account + " " + amount.to_string()) 215 | ) 216 | new_postings_inner.append( 217 | Posting( 218 | account, 219 | units=posting.units._replace(number=(amount.number).quantize(config.quantize)), 220 | cost=posting.cost, 221 | price=None, 222 | flag=None, 223 | meta={} if config.meta_name is None else {config.meta_name: posting.account + " " + amount.to_string()}, 224 | ) 225 | ) 226 | 227 | # 5.3. Handle relative amounts second: create new postings. 228 | remainder = posting.units 229 | total = D(0) 230 | for percent, account in todo_percent: 231 | units = posting.units._replace(number=(D(float(remainder.number) * percent)).quantize(config.quantize)) 232 | total = total + units.number 233 | new_postings_inner.append( 234 | Posting( 235 | account, 236 | units=units, 237 | cost=posting.cost, 238 | price=None, 239 | flag=None, 240 | meta={} 241 | if config.meta_name is None 242 | else { 243 | config.meta_name: posting.account + " " + str(int(percent * 100)) + "% (" + units.to_string() + ")" 244 | }, 245 | ) 246 | ) 247 | if config.meta_name is not None: 248 | posting = posting._replace( 249 | meta=metaset.add( 250 | posting.meta, 251 | config.meta_name, 252 | account + " " + str(int(percent * 100)) + "% (" + units.to_string() + ")", 253 | ) 254 | ) 255 | 256 | # 5.4. Handle absent amounts third: create new postings. 257 | total_percent = sum(i for i, _ in todo_percent) 258 | percent = (1 - total_percent) / (1 + len(todo_absent)) 259 | for account in todo_absent: 260 | units = posting.units._replace(number=(D(float(remainder.number) * percent)).quantize(config.quantize)) 261 | total = total + units.number 262 | new_postings_inner.append( 263 | Posting( 264 | account, 265 | units=units, 266 | cost=posting.cost, 267 | price=None, 268 | flag=None, 269 | meta={} 270 | if config.meta_name is None 271 | else { 272 | config.meta_name: posting.account + " (" + str(int(percent * 100)) + "%, " + units.to_string() + ")" 273 | }, 274 | ) 275 | ) 276 | if config.meta_name is not None: 277 | posting = posting._replace( 278 | meta=metaset.add( 279 | posting.meta, 280 | config.meta_name, 281 | account + " (" + str(int(percent * 100)) + "%, " + units.to_string() + ")", 282 | ) 283 | ) 284 | 285 | # 5.5. Handle original posting last (mutate!). 286 | posting = posting._replace( 287 | units=posting.units._replace(number=(remainder.number - total).quantize(config.quantize)), 288 | meta=metaset.clear(posting.meta, config.mark_name), 289 | ) 290 | 291 | # if(posting.units.number > D(0)): 292 | # new_postings.append(posting) 293 | 294 | return [posting] + new_postings_inner 295 | -------------------------------------------------------------------------------- /beancount_share/utils.py: -------------------------------------------------------------------------------- 1 | from beancount.core.data import Transaction 2 | from beancount.core.inventory import Inventory 3 | 4 | 5 | def sum_income(tx: Transaction) -> Inventory: 6 | total = Inventory() 7 | for posting in tx.postings: 8 | if posting.account.split(":")[0] == "Income": 9 | total.add_position(posting) 10 | return total 11 | 12 | 13 | def sum_expenses(tx: Transaction) -> Inventory: 14 | total = Inventory() 15 | for posting in tx.postings: 16 | if posting.account.split(":")[0] == "Expenses": 17 | total.add_position(posting) 18 | return total 19 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 120 3 | target-version = ['py37'] 4 | include = '\.pyi?$' 5 | exclude = ''' 6 | 7 | ( 8 | /( 9 | \.eggs # exclude a few common directories in the 10 | | \.git # root of the project 11 | | \.hg 12 | | \.mypy_cache 13 | | \.tox 14 | | \.venv 15 | | _build 16 | | buck-out 17 | | build 18 | | dist 19 | )/ 20 | | foo.py # also separately exclude a file named foo.py in 21 | # the root of the project 22 | ) 23 | ''' 24 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | beancount 2 | beancount-plugin-utils 3 | pytest-bdd 4 | pytest-cov 5 | twine 6 | black 7 | setuptools 8 | wheel 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | from setuptools import setup 4 | 5 | # read the contents of your README file 6 | from os import path 7 | this_directory = path.abspath(path.dirname(__file__)) 8 | with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: 9 | long_description = f.read() 10 | 11 | setup( 12 | name='beancount_share', 13 | version='0.1.11', 14 | description='Plugin for Beancount to share expenses.', 15 | 16 | long_description=long_description, 17 | long_description_content_type='text/markdown', 18 | 19 | author='Akuukis', 20 | author_email='akuukis@kalvis.lv', 21 | download_url='https://pypi.python.org/pypi/beancount_share', 22 | license='GNU AGPLv3', 23 | package_data={'beancount_share': ['../README.md', 'requirements.txt']}, 24 | package_dir={'beancount_share': 'beancount_share'}, 25 | packages=['beancount_share'], 26 | install_requires=['beancount >= 2.0', 'beancount_plugin_utils >= 0.0.4'], 27 | url='https://github.com/Akuukis/beancount_share', 28 | ) 29 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | from pytest import fixture 2 | from pytest_bdd import given, when, then, parsers, scenarios 3 | 4 | from beancount.core.data import Transaction 5 | from beancount.core.compare import hash_entry, includes_entries, excludes_entries 6 | from beancount.loader import load_string 7 | from beancount.parser import printer 8 | from beancount_plugin_utils import metaset, marked 9 | from context import share 10 | 11 | def strip_flaky_meta(transaction: Transaction): 12 | transaction = transaction._replace(meta=metaset.discard(transaction.meta, 'filename')) 13 | transaction = transaction._replace(meta=metaset.discard(transaction.meta, 'lineno')) 14 | # new_postings = list(tx.postings) 15 | for j,_ in enumerate(transaction.postings): 16 | transaction.postings[j] = transaction.postings[j]._replace(meta=metaset.discard(transaction.postings[j].meta, 'filename')) 17 | transaction.postings[j] = transaction.postings[j]._replace(meta=metaset.discard(transaction.postings[j].meta, 'lineno')) 18 | transaction.postings[j] = transaction.postings[j]._replace(meta=metaset.discard(transaction.postings[j].meta, '__automatic__')) 19 | # transaction._replace(postings=new_postings) 20 | 21 | return transaction 22 | 23 | 24 | @fixture 25 | def config(): 26 | return "" 27 | 28 | @fixture 29 | def input_txns(): 30 | """ 31 | Returns: 32 | A reference to an empty list. 33 | """ 34 | return list() 35 | 36 | @fixture 37 | def output_txns(): 38 | """ 39 | A fixture used by the when and then steps. 40 | Allows the "then" steps to access the output of the "when" step. 41 | 42 | Returns: 43 | A reference to an empty list. 44 | """ 45 | return list() 46 | 47 | @fixture 48 | def errors(): 49 | return list() 50 | 51 | @given(parsers.parse('this config:' 52 | '{config}')) 53 | def config_custom(config): 54 | pass 55 | 56 | @given(parsers.parse('the following setup:' 57 | '{setup_txns_text}')) 58 | def setup_txns(setup_txns_text): 59 | return setup_txns_text 60 | 61 | 62 | @when(parsers.parse('this transaction is processed:' 63 | '{input_txn_text}')) 64 | def is_processed(input_txns, errors, config, input_txn_text, setup_txns_text, output_txns): 65 | vanilla_text = setup_txns_text + input_txn_text 66 | plugin_text = 'plugin "beancount_share.share" "' + config.strip('\n') + '"\n' + setup_txns_text + input_txn_text 67 | print('\nInput (full & raw):\n------------------------------------------------\n' + plugin_text + '\n') 68 | 69 | input_txns[:], errors[:], _ = load_string(vanilla_text) 70 | print('\nOutput (Transactions without using plugin):\n------------------------------------------------\n') 71 | for txn in input_txns: 72 | print(printer.format_entry(txn)) 73 | for error in errors: 74 | print(printer.format_error(error)) 75 | if len(errors) > 0: 76 | raise Exception('Ledger without plugin already has errors.') 77 | 78 | output_txns[:], errors[:], _ = load_string(plugin_text) 79 | print('\nOutput (Transactions using plugin):\n------------------------------------------------\n') 80 | for txn in output_txns: 81 | print(printer.format_entry(txn)) 82 | print('\nOutput (Errors):\n------------------------------------------------\n') 83 | for error in errors: 84 | print(printer.format_error(error)) 85 | 86 | 87 | @then(parsers.parse('the original transaction should be modified:' 88 | '{correctly_modified_txn_text}')) 89 | def original_txn_modified(output_txns, errors, correctly_modified_txn_text): 90 | # Get modified original transaction from output of plugin 91 | # The modified originial transaction will be the last in the list of output transactions 92 | try: 93 | modified_txn = strip_flaky_meta(output_txns[-1]) 94 | except IndexError as error: 95 | raise error 96 | # Get correctly modified original transaction from feature file 97 | correctly_modified_txn = strip_flaky_meta(load_string(correctly_modified_txn_text)[0][-1]) 98 | 99 | print(" ; RECEIVED:\n", printer.format_entry(modified_txn)) 100 | print(" ; EXPECTED:\n", printer.format_entry(correctly_modified_txn)) 101 | 102 | # Compare strings instead of hashes because that's an easy way to exclude filename & lineno meta. 103 | 104 | try: 105 | print("RECEIVED:\n", modified_txn) 106 | print("EXPECTED:\n", correctly_modified_txn) 107 | assert hash_entry(modified_txn) == hash_entry(correctly_modified_txn) 108 | 109 | except AssertionError: 110 | # Rethrow as a nicely formatted diff 111 | assert printer.format_entry(modified_txn) == '\n'+correctly_modified_txn_text+'\n' 112 | # But in case strings matches.. 113 | raise Exception("Transactions do not match, although their printed output is equal. See log output.") 114 | 115 | @then(parsers.parse('the original transaction should not be modified')) 116 | def tx_not_modified(input_txns, output_txns): 117 | original_txn = strip_flaky_meta(input_txns[-1]) 118 | modified_txn = strip_flaky_meta(output_txns[-1]) 119 | try: 120 | assert hash_entry(original_txn) == hash_entry(modified_txn) 121 | except AssertionError: 122 | print("RECEIVED:", modified_txn) 123 | print("EXPECTED:", original_txn) 124 | # Rethrow as a nicely formatted diff 125 | assert printer.format_entry(modified_txn) == printer.format_entry(original_txn) 126 | # But in case strings matches.. 127 | raise Exception("Transactions do not match, although their printed output is equal. See log output.") 128 | 129 | 130 | 131 | @then(parsers.parse('should not error')) 132 | def not_error(errors): 133 | assert len(errors) == 0 134 | 135 | @then(parsers.parse('should produce config error:' 136 | '{exception_text}')) 137 | def config_error(input_txns, errors, exception_text): 138 | original_txn = input_txns[-1] 139 | assert len(errors) == 1 140 | expected_error = share.PluginShareError(original_txn.meta, exception_text.strip('\n'), original_txn) 141 | assert type(errors[0]) is type(expected_error) 142 | assert errors[0].message == expected_error.message 143 | assert errors[0].entry == None 144 | 145 | @then(parsers.parse('should produce plugin error:' 146 | '{exception_text}')) 147 | def plugin_error(input_txns, errors, exception_text): 148 | original_txn = input_txns[-1] 149 | assert len(errors) == 1 150 | expected_error = share.PluginShareError(original_txn.meta, exception_text.strip('\n'), original_txn) 151 | assert type(errors[0]) is type(expected_error) 152 | assert errors[0].message == expected_error.message 153 | assert strip_flaky_meta(errors[0].entry) == strip_flaky_meta(expected_error.entry) 154 | 155 | @then(parsers.parse('should produce marked error:' 156 | '{exception_text}')) 157 | def marked_error(input_txns, errors, exception_text): 158 | original_txn = input_txns[-1] 159 | assert len(errors) == 1 160 | expected_error = marked.PluginUtilsMarkedError(original_txn.meta, exception_text.strip('\n'), original_txn) 161 | assert type(errors[0]) is type(expected_error) 162 | assert errors[0].message == expected_error.message 163 | assert strip_flaky_meta(errors[0].entry) == strip_flaky_meta(expected_error.entry) 164 | 165 | @then(parsers.parse('should produce beancount error:' 166 | '{exception_text}')) 167 | def beancount_error(input_txns, errors, exception_text, output_txns): 168 | original_txn = input_txns[-1] 169 | modified_txn = output_txns[-1] 170 | assert len(errors) == 1 171 | expected_error = share.PluginShareError(original_txn.meta, exception_text.strip('\n'), original_txn) 172 | assert errors[0].message == expected_error.message and errors[0].entry == modified_txn 173 | -------------------------------------------------------------------------------- /tests/context.py: -------------------------------------------------------------------------------- 1 | # For testsuite, The Way is to use a simple (but explicit) path modification 2 | # to resolve the package properly. 3 | # 4 | # Ref: https://docs.python-guide.org/writing/structure/#test-suite 5 | 6 | import os 7 | import sys 8 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) 9 | 10 | from beancount_share import share 11 | -------------------------------------------------------------------------------- /tests/features/basics.feature: -------------------------------------------------------------------------------- 1 | Feature: Basics 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Assets:Safe 7 | 2020-01-01 open Expenses:Food:Drinks 8 | 2020-01-01 open Income:Random 9 | 10 | Scenario: Skip unmarked transactions. 11 | When this transaction is processed: 12 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 13 | Assets:Cash -10.00 EUR 14 | Expenses:Food:Drinks 15 | 16 | Then the original transaction should not be modified 17 | Then should not error 18 | 19 | Scenario: Skip unmarked transactions even if they have both income nor expense. 20 | When this transaction is processed: 21 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 22 | Income:Random -10.00 EUR 23 | Expenses:Food:Drinks 24 | 25 | Then the original transaction should not be modified 26 | Then should not error 27 | 28 | Scenario: Skip unmarked transactions even if they have no income nor expense. 29 | When this transaction is processed: 30 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 31 | Assets:Cash -10.00 EUR 32 | Assets:Safe 33 | 34 | Then the original transaction should not be modified 35 | Then should not error 36 | -------------------------------------------------------------------------------- /tests/features/configuration.feature: -------------------------------------------------------------------------------- 1 | Feature: Configure plugin behavior 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Expenses:Food:Drinks 7 | 2020-01-01 open Income:Random 8 | 9 | Scenario: Handle default configuration 10 | 11 | Given this config: 12 | { 13 | 'mark_name': 'share', 14 | 'meta_name': 'shared', 15 | 'account_debtors': 'Assets:Debtors', 16 | 'account_creditors': 'Liabilities:Creditors', 17 | 'open_date': '1970-01-01', 18 | 'quantize': '0.01' 19 | } 20 | 21 | When this transaction is processed: 22 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 23 | Assets:Cash -10.00 EUR 24 | Expenses:Food:Drinks 25 | 26 | Then should not error 27 | Then the original transaction should be modified: 28 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 29 | Assets:Cash -10.00 EUR 30 | Expenses:Food:Drinks 5.00 EUR 31 | shared: "Assets:Debtors:Bob (50%, 5.00 EUR)" 32 | Assets:Debtors:Bob 5.00 EUR 33 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 34 | 35 | Scenario: Throw Error if bad config provided 36 | 37 | Given this config: 38 | 'I am not an object' 39 | 40 | When this transaction is processed: 41 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 42 | Assets:Cash -10.00 EUR 43 | Expenses:Food:Drinks 44 | 45 | Then the original transaction should not be modified 46 | And should produce config error: 47 | Plugin configuration must be a dict, skipping.. The config: 'I am not an object' 48 | 49 | Scenario: Throw Error if bad date in the config provided 50 | 51 | Given this config: 52 | {'open_date': 'I am not an UTC date'} 53 | 54 | When this transaction is processed: 55 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 56 | Assets:Cash -10.00 EUR 57 | Expenses:Food:Drinks 58 | 59 | Then the original transaction should not be modified 60 | And should produce config error: 61 | Bad "open_date" value - it must be a valid date, formatted in UTC (e.g. "2000-01-01"). 62 | 63 | Scenario: Throw Error if bad quantize in the config provided 64 | 65 | Given this config: 66 | {'quantize': 'I am not an decimal value'} 67 | 68 | When this transaction is processed: 69 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 70 | Assets:Cash -10.00 EUR 71 | Expenses:Food:Drinks 72 | 73 | Then the original transaction should not be modified 74 | And should produce config error: 75 | Bad "quantize" value - it must be a string that represents a decimal value (e.g. "0.01"). 76 | 77 | Scenario: Rename mark at tag 78 | 79 | Given this config: 80 | {'mark_name': 'asdf'} 81 | 82 | When this transaction is processed: 83 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #asdf-Bob 84 | Assets:Cash -10.00 EUR 85 | Expenses:Food:Drinks 86 | 87 | Then should not error 88 | Then the original transaction should be modified: 89 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 90 | Assets:Cash -10.00 EUR 91 | Expenses:Food:Drinks 5.00 EUR 92 | shared: "Assets:Debtors:Bob (50%, 5.00 EUR)" 93 | Assets:Debtors:Bob 5.00 EUR 94 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 95 | 96 | Scenario: Rename mark at meta 97 | 98 | Given this config: 99 | {'mark_name': 'asdf'} 100 | 101 | When this transaction is processed: 102 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 103 | asdf: "Bob" 104 | Assets:Cash -10.00 EUR 105 | Expenses:Food:Drinks 106 | 107 | Then should not error 108 | Then the original transaction should be modified: 109 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 110 | Assets:Cash -10.00 EUR 111 | Expenses:Food:Drinks 5.00 EUR 112 | shared: "Assets:Debtors:Bob (50%, 5.00 EUR)" 113 | Assets:Debtors:Bob 5.00 EUR 114 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 115 | 116 | Scenario: Disable adding new meta 117 | 118 | Given this config: 119 | {'meta_name': None} 120 | 121 | When this transaction is processed: 122 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 123 | Assets:Cash -10.00 EUR 124 | Expenses:Food:Drinks 125 | 126 | Then should not error 127 | Then the original transaction should be modified: 128 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 129 | Assets:Cash -10.00 EUR 130 | Expenses:Food:Drinks 5.00 EUR 131 | Assets:Debtors:Bob 5.00 EUR 132 | 133 | Scenario: Rename added meta 134 | 135 | Given this config: 136 | {'meta_name': 'asdf'} 137 | 138 | When this transaction is processed: 139 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 140 | Assets:Cash -10.00 EUR 141 | Expenses:Food:Drinks 142 | 143 | Then should not error 144 | Then the original transaction should be modified: 145 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 146 | Assets:Cash -10.00 EUR 147 | Expenses:Food:Drinks 5.00 EUR 148 | asdf: "Assets:Debtors:Bob (50%, 5.00 EUR)" 149 | Assets:Debtors:Bob 5.00 EUR 150 | asdf: "Expenses:Food:Drinks (50%, 5.00 EUR)" 151 | 152 | Scenario: Rename debtor account 153 | 154 | Given this config: 155 | {'account_debtors': 'Assets:EUR:Debtors'} 156 | 157 | When this transaction is processed: 158 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 159 | Assets:Cash -10.00 EUR 160 | Expenses:Food:Drinks 161 | 162 | Then should not error 163 | Then the original transaction should be modified: 164 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 165 | Assets:Cash -10.00 EUR 166 | Expenses:Food:Drinks 5.00 EUR 167 | shared: "Assets:EUR:Debtors:Bob (50%, 5.00 EUR)" 168 | Assets:EUR:Debtors:Bob 5.00 EUR 169 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 170 | 171 | Scenario: Rename debtor account 172 | 173 | Given this config: 174 | {'account_creditors': 'Liabilities:EUR:Creditors'} 175 | 176 | When this transaction is processed: 177 | 2020-01-01 * "BarAlice" "Found change on floor with Bob" 178 | Assets:Cash 10.00 EUR 179 | Income:Random 180 | share: "Bob-40%" 181 | 182 | Then should not error 183 | Then the original transaction should be modified: 184 | 2020-01-01 * "BarAlice" "Found change on floor with Bob" 185 | Assets:Cash 10.00 EUR 186 | Income:Random -6.00 EUR 187 | shared: "Liabilities:EUR:Creditors:Bob 40% (-4.00 EUR)" 188 | Liabilities:EUR:Creditors:Bob -4.00 EUR 189 | shared: "Income:Random 40% (-4.00 EUR)" 190 | 191 | Scenario: Disable creation open entries 192 | 193 | Given this config: 194 | {'open_date': None} 195 | 196 | When this transaction is processed: 197 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 198 | Assets:Cash -10.00 EUR 199 | Expenses:Food:Drinks 200 | 201 | Then the original transaction should be modified: 202 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 203 | Assets:Cash -10.00 EUR 204 | Expenses:Food:Drinks 5.00 EUR 205 | shared: "Assets:Debtors:Bob (50%, 5.00 EUR)" 206 | Assets:Debtors:Bob 5.00 EUR 207 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 208 | 209 | Then should produce beancount error: 210 | Invalid reference to unknown account 'Assets:Debtors:Bob' 211 | -------------------------------------------------------------------------------- /tests/features/currencies.feature: -------------------------------------------------------------------------------- 1 | Feature: Support for currencies 2 | Scenario: Throw Error if more than one currency is detected 3 | -------------------------------------------------------------------------------- /tests/features/errors.feature: -------------------------------------------------------------------------------- 1 | Feature: Report meaningful errors 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Assets:Safe 7 | 2020-01-01 open Expenses:Food:Drinks 8 | 2020-01-01 open Expenses:Food:Lunch 9 | 2020-01-01 open Income:Random 10 | 11 | Scenario: Throw Error if no account provided 12 | When this transaction is processed: 13 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share- 14 | Assets:Cash -10.00 EUR 15 | Expenses:Food:Drinks 16 | 17 | Then the original transaction should not be modified 18 | And should produce plugin error: 19 | Plugin "share" requires mark to contain account name, seperated with "-". 20 | 21 | Scenario: Throw Error for badly formatted abosulute amount 22 | When this transaction is processed: 23 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob-2a50 24 | Assets:Cash -10.00 EUR 25 | Expenses:Food:Drinks 26 | 27 | Then the original transaction should not be modified 28 | And should produce plugin error: 29 | Something wrong with absolute fraction "2a50", please use a dot, e.g. "2.50". 30 | 31 | Scenario: Throw Error for badly formatted relative amount 32 | When this transaction is processed: 33 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob-3a33p 34 | Assets:Cash -10.00 EUR 35 | Expenses:Food:Drinks 36 | 37 | Then the original transaction should not be modified 38 | And should produce plugin error: 39 | Something wrong with relative fraction "3a33p", please use a dot, e.g. "33.33p". 40 | 41 | Scenario: Throw Error if sharing a non-applicable posting (Assets, Liabilities or Equity) 42 | When this transaction is processed: 43 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 44 | Assets:Cash -10.00 EUR 45 | share: "Bob-4" 46 | Expenses:Food:Drinks 47 | 48 | Then the original transaction should not be modified 49 | And should produce marked error: 50 | Mark "share" can be only applied to posting with account types of: ('Income', 'Expenses') 51 | 52 | Scenario: Throw Error if sharing mark has no effect 53 | When this transaction is processed: 54 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" #share-Bob 55 | Assets:Cash -15.00 EUR 56 | Assets:Safe 57 | 58 | Then the original transaction should not be modified 59 | And should produce marked error: 60 | Mark "share" on a transaction has no effect because transaction does not have postings with account types of: ('Income', 'Expenses') 61 | 62 | Scenario: Throw Error if total shared absolute amount is greater than posting amount 63 | When this transaction is processed: 64 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 65 | Assets:Cash -10.00 EUR 66 | Expenses:Food:Drinks 67 | share: "Bob-6" 68 | share2: "Charlie-6" 69 | 70 | Then the original transaction should not be modified 71 | Then should produce plugin error: 72 | The posting can't share more than it's absolute value 73 | 74 | Scenario: Throw Error if total shared relative amount is greater than 100% 75 | When this transaction is processed: 76 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 77 | Assets:Cash -10.00 EUR 78 | Expenses:Food:Drinks 79 | share: "Bob-60%" 80 | share2: "Charlie-60%" 81 | 82 | Then the original transaction should not be modified 83 | And should produce plugin error: 84 | The posting can't share more percent than 100%. 85 | 86 | Scenario: Throw Error if further sharing a posting whose amount is reduced to zero after sharing absolute amounts 87 | When this transaction is processed: 88 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 89 | Assets:Cash -10.00 EUR 90 | Expenses:Food:Drinks 91 | share: "Bob-10" 92 | share2: "Charlie-42%" 93 | 94 | Then the original transaction should not be modified 95 | And should produce plugin error: 96 | It doesn't make sense to split a remaining amount of zero. 97 | 98 | Scenario: Throw Error if further sharing a posting whose amount is already shared by 100% 99 | When this transaction is processed: 100 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 101 | Assets:Cash -10.00 EUR 102 | Expenses:Food:Drinks 103 | share: "Bob-100%" 104 | share2: "Charlie" 105 | 106 | Then the original transaction should not be modified 107 | And should produce plugin error: 108 | It doesn't make sense to further auto-split when amount is already split for full 100%. 109 | 110 | Scenario: Throw Error if sharing both Expense and Income postings 111 | When this transaction is processed: 112 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" 113 | Assets:Cash -15.00 EUR 114 | Expenses:Food:Lunch 17.00 EUR 115 | share: "Bob" 116 | Income:Random -2.00 EUR 117 | share: "Bob" 118 | 119 | Then the original transaction should not be modified 120 | And should produce plugin error: 121 | Plugin "share" doesn't work on transactions that has both income and expense: please split it up into two transactions instead. 122 | -------------------------------------------------------------------------------- /tests/features/many-to-many.feature: -------------------------------------------------------------------------------- 1 | Feature: Share several postings to several accounts 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Expenses:Food:Drinks 7 | 2020-01-01 open Expenses:Food:Lunch 8 | 2020-01-01 open Expenses:Food:Snacks 9 | 10 | Scenario: Share all applicable postings to several overlapping accounts 11 | When this transaction is processed: 12 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" 13 | Assets:Cash -24.00 EUR 14 | Expenses:Food:Lunch 17.00 EUR 15 | share: "Bob-7" 16 | share2: "Charlie" 17 | Expenses:Food:Drinks 18 | share: "Bob" 19 | share2: "Charlie-3" 20 | 21 | Then should not error 22 | Then the original transaction should be modified: 23 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" 24 | Assets:Cash -24.00 EUR 25 | Expenses:Food:Lunch 5.00 EUR 26 | shared: "Assets:Debtors:Bob 7.00 EUR" 27 | shared901: "Assets:Debtors:Charlie (50%, 5.00 EUR)" 28 | Expenses:Food:Drinks 2.00 EUR 29 | shared: "Assets:Debtors:Charlie 3.00 EUR" 30 | shared901: "Assets:Debtors:Bob (50%, 2.00 EUR)" 31 | Assets:Debtors:Bob 9.00 EUR 32 | shared: "Expenses:Food:Lunch 7.00 EUR" 33 | shared901: "Expenses:Food:Drinks (50%, 2.00 EUR)" 34 | Assets:Debtors:Charlie 8.00 EUR 35 | shared: "Expenses:Food:Lunch (50%, 5.00 EUR)" 36 | shared901: "Expenses:Food:Drinks 3.00 EUR" 37 | 38 | Scenario: Share several applicable postings to several overlapping accounts 39 | When this transaction is processed: 40 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" 41 | Assets:Cash -25.00 EUR 42 | Expenses:Food:Snacks 1.00 EUR 43 | Expenses:Food:Lunch 17.00 EUR 44 | share: "Bob-7" 45 | share2: "Charlie" 46 | Expenses:Food:Drinks 47 | share: "Bob" 48 | share2: "Charlie-3" 49 | 50 | Then should not error 51 | Then the original transaction should be modified: 52 | 2020-01-01 * "BarAlice" "Lunch with my guy friends" 53 | Assets:Cash -25.00 EUR 54 | Expenses:Food:Snacks 1.00 EUR 55 | Expenses:Food:Lunch 5.00 EUR 56 | shared: "Assets:Debtors:Bob 7.00 EUR" 57 | shared901: "Assets:Debtors:Charlie (50%, 5.00 EUR)" 58 | Expenses:Food:Drinks 2.00 EUR 59 | shared: "Assets:Debtors:Charlie 3.00 EUR" 60 | shared901: "Assets:Debtors:Bob (50%, 2.00 EUR)" 61 | Assets:Debtors:Charlie 8.00 EUR 62 | shared: "Expenses:Food:Lunch (50%, 5.00 EUR)" 63 | shared901: "Expenses:Food:Drinks 3.00 EUR" 64 | Assets:Debtors:Bob 9.00 EUR 65 | shared: "Expenses:Food:Lunch 7.00 EUR" 66 | shared901: "Expenses:Food:Drinks (50%, 2.00 EUR)" 67 | -------------------------------------------------------------------------------- /tests/features/one-to-many.feature: -------------------------------------------------------------------------------- 1 | Feature: Share a single posting to several accounts 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Expenses:Food:Drinks 7 | 8 | Scenario: Share a posting to several different accounts using absolute amounts 9 | When this transaction is processed: 10 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 11 | Assets:Cash -10.00 EUR 12 | Expenses:Food:Drinks 13 | share: "Bob-4" 14 | share2: "Charlie-4" 15 | 16 | Then should not error 17 | Then the original transaction should be modified: 18 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 19 | Assets:Cash -10.00 EUR 20 | Expenses:Food:Drinks 2.00 EUR 21 | shared: "Assets:Debtors:Bob 4.00 EUR" 22 | shared901: "Assets:Debtors:Charlie 4.00 EUR" 23 | Assets:Debtors:Bob 4.00 EUR 24 | shared: "Expenses:Food:Drinks 4.00 EUR" 25 | Assets:Debtors:Charlie 4.00 EUR 26 | shared: "Expenses:Food:Drinks 4.00 EUR" 27 | 28 | Scenario: Share a posting to several different accounts using relative amounts 29 | When this transaction is processed: 30 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 31 | Assets:Cash -10.00 EUR 32 | Expenses:Food:Drinks 33 | share: "Bob-40%" 34 | share2: "Charlie-40%" 35 | 36 | Then should not error 37 | Then the original transaction should be modified: 38 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 39 | Assets:Cash -10.00 EUR 40 | Expenses:Food:Drinks 2.00 EUR 41 | shared: "Assets:Debtors:Bob 40% (4.00 EUR)" 42 | shared901: "Assets:Debtors:Charlie 40% (4.00 EUR)" 43 | Assets:Debtors:Bob 4.00 EUR 44 | shared: "Expenses:Food:Drinks 40% (4.00 EUR)" 45 | Assets:Debtors:Charlie 4.00 EUR 46 | shared: "Expenses:Food:Drinks 40% (4.00 EUR)" 47 | 48 | Scenario: Share a posting to several different accounts using omitted amount 49 | When this transaction is processed: 50 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 51 | Assets:Cash -10.00 EUR 52 | Expenses:Food:Drinks 53 | share: "Bob" 54 | share2: "Charlie" 55 | 56 | Then should not error 57 | Then the original transaction should be modified: 58 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 59 | Assets:Cash -10.00 EUR 60 | Expenses:Food:Drinks 3.34 EUR 61 | shared: "Assets:Debtors:Bob (33%, 3.33 EUR)" 62 | shared901: "Assets:Debtors:Charlie (33%, 3.33 EUR)" 63 | Assets:Debtors:Bob 3.33 EUR 64 | shared: "Expenses:Food:Drinks (33%, 3.33 EUR)" 65 | Assets:Debtors:Charlie 3.33 EUR 66 | shared: "Expenses:Food:Drinks (33%, 3.33 EUR)" 67 | 68 | Scenario: Share a posting to several different accounts using mixed amounts 69 | When this transaction is processed: 70 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 71 | Assets:Cash -10.00 EUR 72 | Expenses:Food:Drinks 73 | share: "Bob-4" 74 | share2: "Charlie-40%" 75 | share3: "David" 76 | 77 | Then should not error 78 | Then the original transaction should be modified: 79 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 80 | Assets:Cash -10.00 EUR 81 | Expenses:Food:Drinks 1.80 EUR 82 | shared: "Assets:Debtors:Bob 4.00 EUR" 83 | shared901: "Assets:Debtors:Charlie 40% (2.40 EUR)" 84 | shared902: "Assets:Debtors:David (30%, 1.80 EUR)" 85 | Assets:Debtors:Bob 4.00 EUR 86 | shared: "Expenses:Food:Drinks 4.00 EUR" 87 | Assets:Debtors:Charlie 2.40 EUR 88 | shared: "Expenses:Food:Drinks 40% (2.40 EUR)" 89 | Assets:Debtors:David 1.80 EUR 90 | shared: "Expenses:Food:Drinks (30%, 1.80 EUR)" 91 | 92 | Scenario: Share a posting to the same account several times using absolute amounts 93 | When this transaction is processed: 94 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 95 | Assets:Cash -10.00 EUR 96 | Expenses:Food:Drinks 97 | share: "Bob-4" 98 | share2: "Bob-4" 99 | 100 | Then should not error 101 | Then the original transaction should be modified: 102 | 2020-01-01 * "BarAlice" "Beer with my guy friends" 103 | Assets:Cash -10.00 EUR 104 | Expenses:Food:Drinks 2.00 EUR 105 | shared: "Assets:Debtors:Bob 4.00 EUR" 106 | shared901: "Assets:Debtors:Bob 4.00 EUR" 107 | Assets:Debtors:Bob 8.00 EUR 108 | shared: "Expenses:Food:Drinks 4.00 EUR" 109 | shared901: "Expenses:Food:Drinks 4.00 EUR" 110 | 111 | Scenario: Share a posting to the same account several times using relative amounts 112 | When this transaction is processed: 113 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 114 | Assets:Cash -10.00 EUR 115 | Expenses:Food:Drinks 116 | share: "Bob-40%" 117 | share2: "Bob-40%" 118 | 119 | Then should not error 120 | Then the original transaction should be modified: 121 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 122 | Assets:Cash -10.00 EUR 123 | Expenses:Food:Drinks 2.00 EUR 124 | shared: "Assets:Debtors:Bob 40% (4.00 EUR)" 125 | shared901: "Assets:Debtors:Bob 40% (4.00 EUR)" 126 | Assets:Debtors:Bob 8.00 EUR 127 | shared: "Expenses:Food:Drinks 40% (4.00 EUR)" 128 | shared901: "Expenses:Food:Drinks 40% (4.00 EUR)" 129 | 130 | Scenario: Share a posting to the same account several times using omitted amounts 131 | When this transaction is processed: 132 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 133 | Assets:Cash -10.00 EUR 134 | Expenses:Food:Drinks 135 | share: "Bob" 136 | share2: "Bob" 137 | 138 | Then should not error 139 | Then the original transaction should be modified: 140 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 141 | Assets:Cash -10.00 EUR 142 | Expenses:Food:Drinks 3.34 EUR 143 | shared: "Assets:Debtors:Bob (33%, 3.33 EUR)" 144 | shared901: "Assets:Debtors:Bob (33%, 3.33 EUR)" 145 | Assets:Debtors:Bob 6.66 EUR 146 | shared: "Expenses:Food:Drinks (33%, 3.33 EUR)" 147 | shared901: "Expenses:Food:Drinks (33%, 3.33 EUR)" 148 | 149 | Scenario: Share a posting to the same account several times using mixed amounts 150 | When this transaction is processed: 151 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 152 | Assets:Cash -10.00 EUR 153 | Expenses:Food:Drinks 154 | share: "Bob-4" 155 | share2: "Bob-40%" 156 | share3: "Bob" 157 | 158 | Then should not error 159 | Then the original transaction should be modified: 160 | 2020-01-01 * "BarAlice" "Beer with my friend Bob (a lot)" 161 | Assets:Cash -10.00 EUR 162 | Expenses:Food:Drinks 1.80 EUR 163 | shared: "Assets:Debtors:Bob 4.00 EUR" 164 | shared901: "Assets:Debtors:Bob 40% (2.40 EUR)" 165 | shared902: "Assets:Debtors:Bob (30%, 1.80 EUR)" 166 | Assets:Debtors:Bob 8.20 EUR 167 | shared: "Expenses:Food:Drinks 4.00 EUR" 168 | shared901: "Expenses:Food:Drinks 40% (2.40 EUR)" 169 | shared902: "Expenses:Food:Drinks (30%, 1.80 EUR)" 170 | -------------------------------------------------------------------------------- /tests/features/one-to-one.feature: -------------------------------------------------------------------------------- 1 | Feature: Share a single posting to single account 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Expenses:Food:Drinks 7 | 2020-01-01 open Expenses:Food:Snacks 8 | 2020-01-01 open Income:RandomVeryVeryLong 9 | 10 | Scenario: Partially share sole Expense posting using absolute amount 11 | When this transaction is processed: 12 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 13 | Assets:Cash -10.00 EUR 14 | Expenses:Food:Drinks 15 | share: "Assets:Debtors:Bob-4" 16 | 17 | Then should not error 18 | Then the original transaction should be modified: 19 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 20 | Assets:Cash -10.00 EUR 21 | Expenses:Food:Drinks 6.00 EUR 22 | shared: "Assets:Debtors:Bob 4.00 EUR" 23 | Assets:Debtors:Bob 4.00 EUR 24 | shared: "Expenses:Food:Drinks 4.00 EUR" 25 | 26 | Scenario: Partially share sole Expense posting with short account name 27 | When this transaction is processed: 28 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 29 | Assets:Cash -10.00 EUR 30 | Expenses:Food:Drinks 31 | share: "Bob-4" 32 | 33 | Then should not error 34 | Then the original transaction should be modified: 35 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 36 | Assets:Cash -10.00 EUR 37 | Expenses:Food:Drinks 6.00 EUR 38 | shared: "Assets:Debtors:Bob 4.00 EUR" 39 | Assets:Debtors:Bob 4.00 EUR 40 | shared: "Expenses:Food:Drinks 4.00 EUR" 41 | 42 | Scenario: Partially share sole Expense posting using relative amount 43 | When this transaction is processed: 44 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 45 | Assets:Cash -10.00 EUR 46 | Expenses:Food:Drinks 47 | share: "Bob-40%" 48 | 49 | Then should not error 50 | Then the original transaction should be modified: 51 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 52 | Assets:Cash -10.00 EUR 53 | Expenses:Food:Drinks 6.00 EUR 54 | shared: "Assets:Debtors:Bob 40% (4.00 EUR)" 55 | Assets:Debtors:Bob 4.00 EUR 56 | shared: "Expenses:Food:Drinks 40% (4.00 EUR)" 57 | 58 | Scenario: Partially share sole Expense posting using ommitted amount 59 | When this transaction is processed: 60 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 61 | Assets:Cash -10.00 EUR 62 | Expenses:Food:Drinks 63 | share: "Bob" 64 | 65 | Then should not error 66 | Then the original transaction should be modified: 67 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 68 | Assets:Cash -10.00 EUR 69 | Expenses:Food:Drinks 5.00 EUR 70 | shared: "Assets:Debtors:Bob (50%, 5.00 EUR)" 71 | Assets:Debtors:Bob 5.00 EUR 72 | shared: "Expenses:Food:Drinks (50%, 5.00 EUR)" 73 | 74 | Scenario: Fully share sole Expense posting using absolute amount 75 | When this transaction is processed: 76 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 77 | Assets:Cash -10.00 EUR 78 | Expenses:Food:Drinks 79 | share: "Bob-10" 80 | 81 | Then should not error 82 | Then the original transaction should be modified: 83 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 84 | Assets:Cash -10.00 EUR 85 | Expenses:Food:Drinks 0.00 EUR 86 | shared: "Assets:Debtors:Bob 10.00 EUR" 87 | Assets:Debtors:Bob 10.00 EUR 88 | shared: "Expenses:Food:Drinks 10.00 EUR" 89 | 90 | Scenario: Fully share sole Expense posting using relative amount 91 | When this transaction is processed: 92 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 93 | Assets:Cash -10.00 EUR 94 | Expenses:Food:Drinks 95 | share: "Bob-100%" 96 | 97 | Then should not error 98 | Then the original transaction should be modified: 99 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 100 | Assets:Cash -10.00 EUR 101 | Expenses:Food:Drinks 0.00 EUR 102 | shared: "Assets:Debtors:Bob 100% (10.00 EUR)" 103 | Assets:Debtors:Bob 10.00 EUR 104 | shared: "Expenses:Food:Drinks 100% (10.00 EUR)" 105 | 106 | Scenario: Share sole Income posting 107 | When this transaction is processed: 108 | 2020-01-01 * "BarAlice" "Found change on floor with Bob" 109 | Assets:Cash 10.00 EUR 110 | Income:RandomVeryVeryLong 111 | share: "Bob-40%" 112 | 113 | Then should not error 114 | Then the original transaction should be modified: 115 | 2020-01-01 * "BarAlice" "Found change on floor with Bob" 116 | Assets:Cash 10.00 EUR 117 | Income:RandomVeryVeryLong -6.00 EUR 118 | shared: "Liabilities:Creditors:Bob 40% (-4.00 EUR)" 119 | Liabilities:Creditors:Bob -4.00 EUR 120 | shared: "Income:RandomVeryVeryLong 40% (-4.00 EUR)" 121 | 122 | Scenario: Share one of several postings 123 | When this transaction is processed: 124 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 125 | Assets:Cash -12.00 EUR 126 | Expenses:Food:Snacks 2.00 EUR 127 | Expenses:Food:Drinks 128 | share: "Assets:Debtors:Bob-4" 129 | 130 | Then should not error 131 | Then the original transaction should be modified: 132 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 133 | Assets:Cash -12.00 EUR 134 | Expenses:Food:Snacks 2.00 EUR 135 | Expenses:Food:Drinks 6.00 EUR 136 | shared: "Assets:Debtors:Bob 4.00 EUR" 137 | Assets:Debtors:Bob 4.00 EUR 138 | shared: "Expenses:Food:Drinks 4.00 EUR" 139 | -------------------------------------------------------------------------------- /tests/features/shortcuts.feature: -------------------------------------------------------------------------------- 1 | Feature: Shortcuts for marking postings 2 | 3 | Background: default 4 | Given the following setup: 5 | 2020-01-01 open Assets:Cash 6 | 2020-01-01 open Expenses:Food:Drinks 7 | 8 | Scenario: Transaction meta translates to meta for every applicable posting without their own share- meta (positive case) 9 | When this transaction is processed: 10 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 11 | share: "Bob-4" 12 | Assets:Cash -10.00 USD 13 | Expenses:Food:Drinks 14 | 15 | Then should not error 16 | Then the original transaction should be modified: 17 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 18 | Assets:Cash -10.00 USD 19 | Expenses:Food:Drinks 6.00 USD 20 | shared: "Assets:Debtors:Bob 4.00 USD" 21 | Assets:Debtors:Bob 4.00 USD 22 | shared: "Expenses:Food:Drinks 4.00 USD" 23 | 24 | Scenario: Transaction meta translates to meta for every applicable posting without their own share- meta (negative case) 25 | When this transaction is processed: 26 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 27 | share: "Bob-9" 28 | Assets:Cash -10.00 USD 29 | Expenses:Food:Drinks 30 | share: "Bob-4" 31 | 32 | Then should not error 33 | Then the original transaction should be modified: 34 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 35 | Assets:Cash -10.00 USD 36 | Expenses:Food:Drinks 6.00 USD 37 | shared: "Assets:Debtors:Bob 4.00 USD" 38 | Assets:Debtors:Bob 4.00 USD 39 | shared: "Expenses:Food:Drinks 4.00 USD" 40 | 41 | Scenario: Tags are translated and appended to transaction meta 42 | When this transaction is processed: 43 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob-4 44 | Assets:Cash -10.00 USD 45 | Expenses:Food:Drinks 46 | 47 | Then should not error 48 | Then the original transaction should be modified: 49 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 50 | Assets:Cash -10.00 USD 51 | Expenses:Food:Drinks 6.00 USD 52 | shared: "Assets:Debtors:Bob 4.00 USD" 53 | Assets:Debtors:Bob 4.00 USD 54 | shared: "Expenses:Food:Drinks 4.00 USD" 55 | -------------------------------------------------------------------------------- /tests/features/usecases.feature: -------------------------------------------------------------------------------- 1 | Feature: Share expenses with other people easily 2 | 3 | Background: default 4 | 5 | Scenario: Example in this Readme 6 | 7 | Given the following setup: 8 | 2020-01-01 open Assets:Cash 9 | 2020-01-01 open Expenses:Food:Drinks 10 | 11 | When this transaction is processed: 12 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" #share-Bob 13 | Assets:Cash -10.00 USD 14 | Expenses:Food:Drinks 15 | 16 | Then should not error 17 | Then the original transaction should be modified: 18 | 2020-01-01 * "BarAlice" "Lunch with friend Bob" 19 | Assets:Cash -10.00 USD 20 | Expenses:Food:Drinks 5.00 USD 21 | shared: "Assets:Debtors:Bob (50%, 5.00 USD)" 22 | Assets:Debtors:Bob 5.00 USD 23 | shared: "Expenses:Food:Drinks (50%, 5.00 USD)" 24 | 25 | Scenario: Carolyn example in Beancount docs 26 | 27 | Given this config: 28 | { 29 | 'account_debtors': 'Assets:US:Share', 30 | } 31 | Given the following setup: 32 | 2018-01-01 open Liabilities:US:Amex:BlueCash 33 | 2018-01-01 open Expenses:Food:Grocery 34 | 35 | When this transaction is processed: 36 | 2018-12-23 * "WHISK" "Water refill" #share-Carolyn-40p 37 | Liabilities:US:Amex:BlueCash -32.66 USD 38 | Expenses:Food:Grocery 39 | 40 | Then should not error 41 | Then the original transaction should be modified: 42 | 2018-12-23 * "WHISK" "Water refill" 43 | Liabilities:US:Amex:BlueCash -32.66 USD 44 | Expenses:Food:Grocery 19.60 USD 45 | shared: "Assets:US:Share:Carolyn 40% (13.06 USD)" 46 | Assets:US:Share:Carolyn 13.06 USD 47 | shared: "Expenses:Food:Grocery 40% (13.06 USD)" 48 | 49 | Scenario: Kyle example in Beancount docs 50 | 51 | Given this config: 52 | { 53 | 'account_debtors': 'Expenses', 54 | } 55 | Given the following setup: 56 | 2018-01-01 open Liabilities:US:Amex:BlueCash 57 | 2018-01-01 open Expenses:Pharmacy 58 | 59 | When this transaction is processed: 60 | 2019-02-01 * "AMAZON.COM" "MERCHANDISE - Diapers size 4 for Kyle" #share-Kyle-100p 61 | Liabilities:US:Amex:BlueCash -49.99 USD 62 | Expenses:Pharmacy 63 | 64 | Then should not error 65 | Then the original transaction should be modified: 66 | 2019-02-01 * "AMAZON.COM" "MERCHANDISE - Diapers size 4 for Kyle" 67 | Liabilities:US:Amex:BlueCash -49.99 USD 68 | Expenses:Pharmacy 0.00 USD 69 | shared: "Expenses:Kyle 100% (49.99 USD)" 70 | Expenses:Kyle 49.99 USD 71 | shared: "Expenses:Pharmacy 100% (49.99 USD)" 72 | 73 | 74 | Scenario: Bug No 1: handle padding 75 | Given the following setup: 76 | 2020-01-01 open Equity:Opening-Balances CHF 77 | 2020-01-01 open Assets:Bank CHF 78 | 2020-01-01 open Assets:Savings CHF 79 | 2020-01-01 pad Assets:Bank Equity:Opening-Balances 80 | 2020-02-02 balance Assets:Bank 2000 CHF 81 | 82 | When this transaction is processed: 83 | 2020-11-17 * "Savings" 84 | Assets:Savings 1000 CHF 85 | Assets:Bank -1000 CHF 86 | 87 | Then should not error 88 | 89 | 90 | Scenario: Bug No 3: unquoted dict keys 91 | Given this config: 92 | { 93 | mark_name: 'share', 94 | meta_name: None, 95 | account_debtors: 'Liabilities:Receivables', 96 | account_creditors: 'Liabilities:Receivables', 97 | open_date: None 98 | } 99 | Given the following setup: 100 | 2020-01-01 open Expenses:Food:Takeout 101 | 2020-01-01 open Liabilities:Receivables:Gia 102 | 2020-01-01 open Liabilities:US:Chase:Freedom USD 103 | 104 | When this transaction is processed: 105 | 2020-02-01 * "Takeout" "Food" #share-Gia 106 | Liabilities:US:Chase:Freedom -50.00 USD 107 | Expenses:Food:Takeout 108 | 109 | Then the original transaction should not be modified 110 | And should produce config error: 111 | Failed to parse plugin configuration, skipping.. The config: { 112 | mark_name: 'share', 113 | meta_name: None, 114 | account_debtors: 'Liabilities:Receivables', 115 | account_creditors: 'Liabilities:Receivables', 116 | open_date: None 117 | } 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /tests/test_all.py: -------------------------------------------------------------------------------- 1 | from pytest_bdd import scenarios 2 | 3 | import conftest 4 | 5 | scenarios('features/basics.feature') 6 | scenarios('features/one-to-one.feature') 7 | scenarios('features/one-to-many.feature') 8 | scenarios('features/many-to-many.feature') 9 | scenarios('features/shortcuts.feature') 10 | scenarios('features/currencies.feature') 11 | scenarios('features/configuration.feature') 12 | scenarios('features/errors.feature') 13 | scenarios('features/usecases.feature') 14 | --------------------------------------------------------------------------------