|
| 1 | +# Erpnext notes |
| 2 | + |
| 3 | +I mainly use the Docker installation of ERPNext, so these notes are focused on that setup. |
| 4 | + |
| 5 | +## General commands for Frappe/ERPNext with Docker |
| 6 | + |
| 7 | +### Get a python prompt inside a running container |
| 8 | + |
| 9 | +```sh |
| 10 | +sudo docker exec -it container_name_backend bench --site frontend console |
| 11 | +``` |
| 12 | + |
| 13 | +### When updating |
| 14 | + |
| 15 | +If the image is updated, the database needs to be migrated. |
| 16 | + |
| 17 | +```sh |
| 18 | +sudo docker exec -it container_name_backend bench --site frontend migrate |
| 19 | +``` |
| 20 | + |
| 21 | +### When frontend is not loading properly |
| 22 | + |
| 23 | +```sh |
| 24 | +sudo docker exec -it container_name_backend bench --site frontend clear-cache |
| 25 | +sudo docker exec -it container_name_backend bench --site frontend clear-website-cache |
| 26 | +sudo docker exec -it container_name_redis_queue redis-cli flushall |
| 27 | +sudo docker exec -it container_name_redis_cache redis-cli flushall |
| 28 | +``` |
| 29 | + |
| 30 | +### Change variant template |
| 31 | + |
| 32 | +Sometimes in ERPNext, an item variant may be incorrectly linked to the wrong template. The following Python scripts can be used within the Frappe framework to correct this issue. |
| 33 | + |
| 34 | +**If the item has no links:** |
| 35 | + |
| 36 | +```python |
| 37 | +def update_variant_template(variant_to_fix, correct_template): |
| 38 | + # Fetch the variant document |
| 39 | + doc = frappe.get_doc("Item", variant_to_fix) |
| 40 | + |
| 41 | + # Update the variant_of field |
| 42 | + doc.variant_of = correct_template |
| 43 | + |
| 44 | + # Save the changes |
| 45 | + doc.save() |
| 46 | + |
| 47 | + # Commit to apply changes to the database |
| 48 | + frappe.db.commit() |
| 49 | + |
| 50 | + print(f"Updated Item {variant_to_fix}: Now a variant of {correct_template}") |
| 51 | +``` |
| 52 | + |
| 53 | +**If the item has links and cannot be changed directly:** |
| 54 | + |
| 55 | +```python |
| 56 | +def clone_variant_fix_template(old_variant, correct_template): |
| 57 | + # Fetch existing variant |
| 58 | + old_doc = frappe.get_doc("Item", old_variant) |
| 59 | + |
| 60 | + # Generate a new unique item code |
| 61 | + new_item_code = make_autoname(f"{old_variant}-.#####") |
| 62 | + |
| 63 | + # Create a new variant with the correct template |
| 64 | + new_doc = frappe.copy_doc(old_doc) |
| 65 | + new_doc.variant_of = correct_template |
| 66 | + new_doc.item_code = new_item_code # Assign a unique item code |
| 67 | + new_doc.insert() |
| 68 | + |
| 69 | + # Deactivate old variant to prevent further usage |
| 70 | + old_doc.disabled = 1 |
| 71 | + old_doc.save() |
| 72 | + |
| 73 | + # Commit to apply changes to the database |
| 74 | + frappe.db.commit() |
| 75 | + |
| 76 | + print(f"Created new variant {new_doc.name} ({new_item_code}) under {correct_template} and disabled {old_variant}.") |
| 77 | +``` |
| 78 | + |
| 79 | +**Example usage:** |
| 80 | + |
| 81 | +```python |
| 82 | +# Update a single variant |
| 83 | +update_variant_template("variant_code", "template_code") |
| 84 | + |
| 85 | +# Clone a variant and fix the template |
| 86 | +clone_variant_fix_template("variant_code", "template_code") |
| 87 | +``` |
| 88 | + |
| 89 | + |
0 commit comments