import re
with open('stock.php', 'r') as f:
content = f.read()
# Update openItemModal definition
content = content.replace(
"function openItemModal(sku = '', name = '', price = '', cost_price = '', base_stock = '', vat = '= h(get_setting('vat_percentage', 5)) ?>', category_id = '', supplier_id = '', unit_id = '', image_url = '') {",
"function openItemModal(sku = '', name = '', price = '', cost_price = '', base_stock = '', vat = '= h(get_setting('vat_percentage', 5)) ?>', category_id = '', supplier_id = '', unit_id = '', image_url = '', in_catalog = 0) {"
)
# Add UI toggle in the form
switch_html = """
"""
content = content.replace(
'\n
',
switch_html + '
\n '
)
# Update Javascript form handling
content = content.replace(
"document.getElementById('item_unit').value = unit_id;",
"document.getElementById('item_unit').value = unit_id;\n document.getElementById('item_in_catalog').checked = (parseInt(in_catalog) === 1);"
)
content = content.replace(
"formData.append('unit_id', document.getElementById('item_unit').value);",
"formData.append('unit_id', document.getElementById('item_unit').value);\n formData.append('in_catalog', document.getElementById('item_in_catalog').checked ? '1' : '0');"
)
# Replace the onclick in the button where we pass the arguments
search_str = "onclick=\"openItemModal('= h($row['sku']) ?>', '= h(addslashes($row['name'])) ?>', '= h($row['price']) ?>', '= h($row['cost_price'] ?? 0) ?>', '= h($row['base_stock']) ?>', '= h($row['vat'] ?? get_setting('vat_percentage', 5)) ?>', '= h($row['category_id'] ?? '') ?>', '= h($row['supplier_id'] ?? '') ?>', '= h($row['unit_id'] ?? '') ?>', '= h($row['image_url'] ?? '') ?>')\""
replace_str = "onclick=\"openItemModal('= h($row['sku']) ?>', '= h(addslashes($row['name'])) ?>', '= h($row['price']) ?>', '= h($row['cost_price'] ?? 0) ?>', '= h($row['base_stock']) ?>', '= h($row['vat'] ?? get_setting('vat_percentage', 5)) ?>', '= h($row['category_id'] ?? '') ?>', '= h($row['supplier_id'] ?? '') ?>', '= h($row['unit_id'] ?? '') ?>', '= h($row['image_url'] ?? '') ?>', '= h($row['in_catalog'] ?? 0) ?>')\""
content = content.replace(search_str, replace_str)
with open('stock.php', 'w') as f:
f.write(content)