39728-vm/patch_stock.py
2026-04-19 16:03:44 +00:00

45 lines
3.1 KiB
Python

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 = """
<div class="col-md-6 mb-3">
<label class="form-label"><?= h(tr('الظهور في المتجر (الكتالوج)', 'Visible in Catalog')) ?></label>
<div class="form-check form-switch mt-2">
<input class="form-check-input" type="checkbox" id="item_in_catalog" style="transform: scale(1.3); margin-left: -1.5em; margin-right: 0.5em;">
<label class="form-check-label" for="item_in_catalog" style="margin-right: 2.5em;"><?= h(tr('عرض أونلاين', 'Show Online')) ?></label>
</div>
</div>
"""
content = content.replace(
'<div class="col-md-6 mb-3">\n <label class="form-label"><?= h(tr(\'التكلفة\', \'Cost Price\')) ?></label>',
switch_html + '<div class="col-md-6 mb-3">\n <label class="form-label"><?= h(tr(\'التكلفة\', \'Cost Price\')) ?></label>'
)
# 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)