add template get
This commit is contained in:
parent
702e718431
commit
cb59dd65ca
5 changed files with 246 additions and 24 deletions
|
|
@ -260,8 +260,81 @@ def update_response(name, response):
|
|||
html = frappe.render_template(template, {"error": str(e)})
|
||||
return Response(html, mimetype="text/html")
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_estimate_templates(company):
|
||||
"""Get available estimate templates."""
|
||||
filters = {"is_active": 1}
|
||||
if company:
|
||||
filters["company"] = company
|
||||
try:
|
||||
print("DEBUG: Fetching estimate templates for company:", company)
|
||||
templates = frappe.get_all("Quotation Template", fields=["name", "is_active", "description"], filters=filters)
|
||||
|
||||
result = []
|
||||
if not templates:
|
||||
print("DEBUG: No templates found.")
|
||||
return build_success_response(result)
|
||||
for template in templates:
|
||||
items = frappe.get_all("Quotation Template Item",
|
||||
fields=["item_code", "item_name", "description", "qty", "discount_percentage", "rate"],
|
||||
filters={"parent": template.name},
|
||||
order_by="idx")
|
||||
|
||||
# Map fields to camelCase as requested
|
||||
mapped_items = []
|
||||
for item in items:
|
||||
mapped_items.append({
|
||||
"itemCode": item.item_code,
|
||||
"itemName": item.item_name,
|
||||
"description": item.description,
|
||||
"quantity": item.qty,
|
||||
"discountPercentage": item.discount_percentage,
|
||||
"rate": item.rate
|
||||
})
|
||||
|
||||
result.append({
|
||||
"templateName": template.name,
|
||||
"active": template.active,
|
||||
"description": template.description,
|
||||
"items": mapped_items
|
||||
})
|
||||
|
||||
return build_success_response(result)
|
||||
except Exception as e:
|
||||
return build_error_response(str(e), 500)
|
||||
|
||||
@frappe.whitelist()
|
||||
def create_template(data):
|
||||
"""Create a new estimate template."""
|
||||
try:
|
||||
data = json.loads(data) if isinstance(data, str) else data
|
||||
print("DEBUG: Creating estimate template with data:", data)
|
||||
|
||||
new_template = frappe.get_doc({
|
||||
"doctype": "Quotation Template",
|
||||
"template_name": data.get("templateName"),
|
||||
"is_active": data.get("active", 1),
|
||||
"description": data.get("description", ""),
|
||||
"company": data.get("company", ""),
|
||||
"source_quotation": data.get("source_quotation", "")
|
||||
})
|
||||
|
||||
for item in data.get("items", []):
|
||||
item = json.loads(item) if isinstance(item, str) else item
|
||||
new_template.append("items", {
|
||||
"item_code": item.get("itemCode"),
|
||||
"item_name": item.get("itemName"),
|
||||
"description": item.get("description"),
|
||||
"qty": item.get("quantity"),
|
||||
"discount_percentage": item.get("discountPercentage"),
|
||||
"rate": item.get("rate")
|
||||
})
|
||||
|
||||
new_template.insert()
|
||||
print("DEBUG: New estimate template created with name:", new_template.name)
|
||||
return build_success_response(new_template.as_dict())
|
||||
except Exception as e:
|
||||
return build_error_response(str(e), 500)
|
||||
|
||||
@frappe.whitelist()
|
||||
def upsert_estimate(data):
|
||||
|
|
|
|||
|
|
@ -413,6 +413,70 @@
|
|||
"trigger": null,
|
||||
"unique": 0,
|
||||
"width": null
|
||||
},
|
||||
{
|
||||
"allow_bulk_edit": 0,
|
||||
"allow_in_quick_entry": 0,
|
||||
"allow_on_submit": 0,
|
||||
"bold": 0,
|
||||
"collapsible": 0,
|
||||
"collapsible_depends_on": null,
|
||||
"columns": 0,
|
||||
"default": null,
|
||||
"depends_on": null,
|
||||
"description": null,
|
||||
"documentation_url": null,
|
||||
"fetch_from": null,
|
||||
"fetch_if_empty": 0,
|
||||
"fieldname": "company",
|
||||
"fieldtype": "Link",
|
||||
"hidden": 0,
|
||||
"hide_border": 0,
|
||||
"hide_days": 0,
|
||||
"hide_seconds": 0,
|
||||
"ignore_user_permissions": 0,
|
||||
"ignore_xss_filter": 0,
|
||||
"in_filter": 0,
|
||||
"in_global_search": 0,
|
||||
"in_list_view": 0,
|
||||
"in_preview": 0,
|
||||
"in_standard_filter": 0,
|
||||
"is_virtual": 0,
|
||||
"label": "Company",
|
||||
"length": 0,
|
||||
"link_filters": null,
|
||||
"make_attachment_public": 0,
|
||||
"mandatory_depends_on": null,
|
||||
"max_height": null,
|
||||
"no_copy": 0,
|
||||
"non_negative": 0,
|
||||
"oldfieldname": null,
|
||||
"oldfieldtype": null,
|
||||
"options": "Company",
|
||||
"parent": "Quotation Template",
|
||||
"parentfield": "fields",
|
||||
"parenttype": "DocType",
|
||||
"permlevel": 0,
|
||||
"placeholder": null,
|
||||
"precision": "",
|
||||
"print_hide": 0,
|
||||
"print_hide_if_no_value": 0,
|
||||
"print_width": null,
|
||||
"read_only": 0,
|
||||
"read_only_depends_on": null,
|
||||
"remember_last_selected_value": 0,
|
||||
"report_hide": 0,
|
||||
"reqd": 0,
|
||||
"search_index": 0,
|
||||
"set_only_once": 0,
|
||||
"show_dashboard": 0,
|
||||
"show_on_timeline": 0,
|
||||
"show_preview_popup": 0,
|
||||
"sort_options": 0,
|
||||
"translatable": 0,
|
||||
"trigger": null,
|
||||
"unique": 0,
|
||||
"width": null
|
||||
}
|
||||
],
|
||||
"force_re_route_to_default_view": 0,
|
||||
|
|
@ -435,7 +499,7 @@
|
|||
"max_attachments": 0,
|
||||
"menu_index": null,
|
||||
"migration_hash": null,
|
||||
"modified": "2025-12-23 02:03:44.840865",
|
||||
"modified": "2026-01-02 11:26:31.164108",
|
||||
"module": "Selling",
|
||||
"name": "Quotation Template",
|
||||
"naming_rule": "",
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ const FRAPPE_GET_ESTIMATE_BY_ADDRESS_METHOD = "custom_ui.api.db.estimates.get_es
|
|||
const FRAPPE_SEND_ESTIMATE_EMAIL_METHOD = "custom_ui.api.db.estimates.send_estimate_email";
|
||||
const FRAPPE_LOCK_ESTIMATE_METHOD = "custom_ui.api.db.estimates.lock_estimate";
|
||||
const FRAPPE_ESTIMATE_UPDATE_RESPONSE_METHOD = "custom_ui.api.db.estimates.manual_response";
|
||||
const FRAPPE_GET_ESTIMATE_TEMPLATES_METHOD = "custom_ui.api.db.estimates.get_estimate_templates";
|
||||
// Job methods
|
||||
const FRAPPE_GET_JOB_METHOD = "custom_ui.api.db.jobs.get_job";
|
||||
const FRAPPE_GET_JOBS_METHOD = "custom_ui.api.db.jobs.get_jobs_table_data";
|
||||
|
|
@ -222,6 +223,10 @@ class Api {
|
|||
return await this.request(FRAPPE_ESTIMATE_UPDATE_RESPONSE_METHOD, {name: estimateName, response});
|
||||
}
|
||||
|
||||
static async getEstimateTemplates(company) {
|
||||
return await this.request(FRAPPE_GET_ESTIMATE_TEMPLATES_METHOD, { company });
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// JOB / PROJECT METHODS
|
||||
// ============================================================================
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
<template>
|
||||
<div>
|
||||
<h3>History</h3>
|
||||
<Tabs value="0" class="tabs">
|
||||
<TabList>
|
||||
|
|
@ -19,6 +20,7 @@
|
|||
</TabPanel>
|
||||
</TabPanels>
|
||||
</Tabs>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
|
|
|
|||
|
|
@ -59,6 +59,27 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Template Section -->
|
||||
<div v-if="isNew" class="template-section">
|
||||
<label for="template" class="field-label">Template</label>
|
||||
<Select
|
||||
v-model="selectedTemplate"
|
||||
:options="templates"
|
||||
optionLabel="templateName"
|
||||
optionValue="templateName"
|
||||
placeholder="Select a template"
|
||||
fluid
|
||||
@change="onTemplateChange"
|
||||
>
|
||||
<template #option="slotProps">
|
||||
<div class="template-option">
|
||||
<div class="template-name">{{ slotProps.option.templateName }}</div>
|
||||
<div class="template-desc">{{ slotProps.option.description }}</div>
|
||||
</div>
|
||||
</template>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<!-- Items Section -->
|
||||
<div class="items-section">
|
||||
<h3>Items</h3>
|
||||
|
|
@ -334,6 +355,8 @@ const contactOptions = ref([]);
|
|||
const quotationItems = ref([]);
|
||||
const selectedItems = ref([]);
|
||||
const responses = ref(["Accepted", "Rejected"]);
|
||||
const templates = ref([]);
|
||||
const selectedTemplate = ref(null);
|
||||
|
||||
const showAddressModal = ref(false);
|
||||
const showAddItemModal = ref(false);
|
||||
|
|
@ -359,6 +382,38 @@ const itemColumns = [
|
|||
];
|
||||
|
||||
// Methods
|
||||
const fetchTemplates = async () => {
|
||||
if (!isNew.value) return;
|
||||
try {
|
||||
const result = await Api.getEstimateTemplates(company.currentCompany);
|
||||
templates.value = result;
|
||||
} catch (error) {
|
||||
console.error("Error fetching templates:", error);
|
||||
notificationStore.addNotification("Failed to fetch templates", "error");
|
||||
}
|
||||
};
|
||||
|
||||
const onTemplateChange = () => {
|
||||
const template = templates.value.find(t => t.templateName === selectedTemplate.value);
|
||||
if (template && template.items) {
|
||||
selectedItems.value = template.items.map(item => ({
|
||||
itemCode: item.itemCode,
|
||||
itemName: item.itemName,
|
||||
qty: item.quantity,
|
||||
standardRate: item.rate,
|
||||
discountAmount: null,
|
||||
discountPercentage: item.discountPercentage,
|
||||
discountType: item.discountPercentage > 0 ? 'percentage' : 'currency'
|
||||
}));
|
||||
// Calculate discount amounts
|
||||
selectedItems.value.forEach(item => {
|
||||
if (item.discountType === 'percentage') {
|
||||
updateDiscountFromPercentage(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const searchAddresses = async () => {
|
||||
const searchTerm = formData.address.trim();
|
||||
if (!searchTerm) return;
|
||||
|
|
@ -574,6 +629,10 @@ watch(
|
|||
},
|
||||
);
|
||||
|
||||
watch(() => company.currentCompany, () => {
|
||||
fetchTemplates();
|
||||
});
|
||||
|
||||
// Watch for query param changes to refresh page behavior
|
||||
watch(
|
||||
() => route.query,
|
||||
|
|
@ -672,6 +731,10 @@ onMounted(async () => {
|
|||
console.error("Error loading quotation items:", error);
|
||||
}
|
||||
|
||||
if (isNew.value) {
|
||||
fetchTemplates();
|
||||
}
|
||||
|
||||
if (addressQuery.value && isNew.value) {
|
||||
// Creating new estimate - pre-fill address
|
||||
await selectAddress(addressQuery.value);
|
||||
|
|
@ -747,7 +810,8 @@ onMounted(async () => {
|
|||
}
|
||||
|
||||
.address-section,
|
||||
.contact-section {
|
||||
.contact-section,
|
||||
.template-section {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
|
|
@ -1014,5 +1078,19 @@ onMounted(async () => {
|
|||
font-size: 0.9em;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.template-option {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.template-name {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.template-desc {
|
||||
font-size: 0.85rem;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
<parameter name="filePath"></parameter>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue