feat: Make post_depreciation.py configurable via command-line arguments for Odoo path, config, database, and date, and update the README with the new usage.

This commit is contained in:
Suherdy Yacob 2026-01-22 08:24:14 +07:00
parent 3803078318
commit cca0e8d88e
2 changed files with 61 additions and 23 deletions

View File

@ -38,9 +38,21 @@ Posts all **Draft** depreciation entries found in the system up to a specific da
### Usage ### Usage
```bash ```bash
/home/suherdy/Pythoncode/odoo17/.venv/bin/python scripts/post_depreciation.py /path/to/venv/bin/python scripts/post_depreciation.py \
<path_to_odoo_bin> \
<path_to_odoo_conf> \
<database_name> \
[--date YYYY-MM-DD]
```
**Example:**
```bash
/home/suherdy/Pythoncode/odoo17/.venv/bin/python scripts/post_depreciation.py \
/home/suherdy/Pythoncode/odoo17/odoo/odoo-bin \
/home/suherdy/Pythoncode/odoo17/odoo.conf \
kipasdbclone5 \
--date 2026-01-21
``` ```
*Note: You may need to edit the `POST_UP_TO_DATE` variable inside the script to target a specific date.*
--- ---

View File

@ -1,28 +1,54 @@
import sys import sys
import os import os
from datetime import date import argparse
from datetime import date, datetime
# ---------------- CONFIGURATION ---------------- def main():
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) parser = argparse.ArgumentParser(description="Post Draft Depreciation Entries in Odoo")
# Change CWD to Project Root so relative paths in odoo.conf work parser.add_argument("odoo_bin_path", help="Path to odoo-bin executable")
os.chdir(PROJECT_ROOT) parser.add_argument("conf_path", help="Path to odoo.conf")
parser.add_argument("db_name", help="Database name")
parser.add_argument("--date", help="Post entries up to this date (YYYY-MM-DD). Defaults to today.", default=None)
ODOO_PATH = os.path.join(PROJECT_ROOT, 'odoo') args = parser.parse_args()
CONF_FILE = os.path.join(PROJECT_ROOT, 'odoo.conf')
DB_NAME = 'kipasdbclone5'
POST_UP_TO_DATE = date(2026, 1, 21) # User specified "now"
if ODOO_PATH not in sys.path: odoo_bin_path = os.path.abspath(args.odoo_bin_path)
sys.path.append(ODOO_PATH) conf_path = os.path.abspath(args.conf_path)
db_name = args.db_name
import odoo if args.date:
from odoo import api, SUPERUSER_ID try:
post_up_to_date = datetime.strptime(args.date, '%Y-%m-%d').date()
except ValueError:
print("Error: Date must be in YYYY-MM-DD format.")
sys.exit(1)
else:
post_up_to_date = date.today()
# Add Odoo to sys.path
odoo_root = os.path.dirname(odoo_bin_path)
if odoo_root not in sys.path:
sys.path.append(odoo_root)
# Change CWD to config directory to handle relative paths in config
os.chdir(os.path.dirname(conf_path))
def post_depreciation():
print(f"Initializing Odoo Environment for database: {DB_NAME}...")
try: try:
odoo.tools.config.parse_config(['-c', CONF_FILE]) import odoo
registry = odoo.registry(DB_NAME) from odoo import api, SUPERUSER_ID
except ImportError:
print(f"Error: Could not import 'odoo' module from {odoo_root}. Make sure odoo-bin path is correct.")
sys.exit(1)
post_depreciation(db_name, conf_path, post_up_to_date)
def post_depreciation(db_name, conf_file, post_up_to_date):
print(f"Initializing Odoo Environment for database: {db_name}...")
try:
import odoo
from odoo import api, SUPERUSER_ID
odoo.tools.config.parse_config(['-c', conf_file])
registry = odoo.registry(db_name)
except Exception as e: except Exception as e:
print(f"Error initializing Odoo: {e}") print(f"Error initializing Odoo: {e}")
return return
@ -36,14 +62,14 @@ def post_depreciation():
moves_to_post = env['account.move'].search([ moves_to_post = env['account.move'].search([
('asset_id', '!=', False), ('asset_id', '!=', False),
('state', '=', 'draft'), ('state', '=', 'draft'),
('date', '<=', POST_UP_TO_DATE) ('date', '<=', post_up_to_date)
]) ])
if not moves_to_post: if not moves_to_post:
print(f"No draft depreciation entries found on or before {POST_UP_TO_DATE}.") print(f"No draft depreciation entries found on or before {post_up_to_date}.")
return return
print(f"Found {len(moves_to_post)} moves to post up to {POST_UP_TO_DATE}.") print(f"Found {len(moves_to_post)} moves to post up to {post_up_to_date}.")
# Group by date for nicer logging # Group by date for nicer logging
moves_by_date = {} moves_by_date = {}
@ -72,4 +98,4 @@ def post_depreciation():
traceback.print_exc() traceback.print_exc()
if __name__ == "__main__": if __name__ == "__main__":
post_depreciation() main()