Compare commits

...

2 commits

Author SHA1 Message Date
97241f14ea create tempalte functionality 2026-01-02 16:28:57 -06:00
cb59dd65ca add template get 2026-01-02 15:55:27 -06:00
6 changed files with 416 additions and 22 deletions

View file

@ -260,8 +260,119 @@ def update_response(name, response):
html = frappe.render_template(template, {"error": str(e)}) html = frappe.render_template(template, {"error": str(e)})
return Response(html, mimetype="text/html") 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=["*"], filters=filters)
result = []
if not templates:
print("DEBUG: No templates found.")
return build_success_response(result)
print(f"DEBUG: Found {len(templates)} templates.")
for template in templates:
print("DEBUG: Processing template:", template)
items = frappe.get_all("Quotation Template Item",
fields=["item_code", "item_name", "description", "quantity", "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({
"name": template.name,
"templateName": template.template_name,
"active": template.is_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_estimate_template(data):
"""Create a new estimate template."""
try:
print("DEBUG: Creating estimate template with data:", data)
data = json.loads(data) if isinstance(data, str) else data
doc_data = {
"doctype": "Quotation Template",
"is_active": 1,
"description": data.get("description"),
"company": data.get("company"),
"items": [],
"template_name": data.get("template_name"),
"source_quotation": data.get("source_quotation", "")
}
new_template = frappe.get_doc(doc_data)
for item in data.get("items", []):
new_template.append("items", {
"item_code": item.get("item_code"),
"item_name": item.get("item_name"),
"description": item.get("description"),
"qty": item.get("qty") or item.get("quantity"),
"rate": item.get("standard_rate") or item.get("rate"),
"discount_percentage": item.get("discount_percentage")
})
new_template.insert()
return build_success_response(new_template.name)
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() @frappe.whitelist()
def upsert_estimate(data): def upsert_estimate(data):

View file

@ -413,6 +413,70 @@
"trigger": null, "trigger": null,
"unique": 0, "unique": 0,
"width": null "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, "force_re_route_to_default_view": 0,
@ -435,7 +499,7 @@
"max_attachments": 0, "max_attachments": 0,
"menu_index": null, "menu_index": null,
"migration_hash": null, "migration_hash": null,
"modified": "2025-12-23 02:03:44.840865", "modified": "2026-01-02 11:26:31.164108",
"module": "Selling", "module": "Selling",
"name": "Quotation Template", "name": "Quotation Template",
"naming_rule": "", "naming_rule": "",

View file

@ -12,6 +12,8 @@ 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_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_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_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";
const FRAPPE_CREATE_ESTIMATE_TEMPLATE_METHOD = "custom_ui.api.db.estimates.create_estimate_template";
// Job methods // Job methods
const FRAPPE_GET_JOB_METHOD = "custom_ui.api.db.jobs.get_job"; 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"; const FRAPPE_GET_JOBS_METHOD = "custom_ui.api.db.jobs.get_jobs_table_data";
@ -222,6 +224,14 @@ class Api {
return await this.request(FRAPPE_ESTIMATE_UPDATE_RESPONSE_METHOD, {name: estimateName, response}); 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 });
}
static async createEstimateTemplate(data) {
return await this.request(FRAPPE_CREATE_ESTIMATE_TEMPLATE_METHOD, { data });
}
// ============================================================================ // ============================================================================
// JOB / PROJECT METHODS // JOB / PROJECT METHODS
// ============================================================================ // ============================================================================

View file

@ -1,4 +1,5 @@
<template> <template>
<div>
<h3>History</h3> <h3>History</h3>
<Tabs value="0" class="tabs"> <Tabs value="0" class="tabs">
<TabList> <TabList>
@ -19,6 +20,7 @@
</TabPanel> </TabPanel>
</TabPanels> </TabPanels>
</Tabs> </Tabs>
</div>
</template> </template>
<script setup> <script setup>

View file

@ -0,0 +1,82 @@
<template>
<Modal
:visible="visible"
@update:visible="$emit('update:visible', $event)"
@close="$emit('update:visible', false)"
:options="{ showActions: false }"
>
<template #title>Save As Template</template>
<div class="modal-content">
<div class="field-group">
<label for="templateName" class="field-label">Template Name</label>
<InputText id="templateName" v-model="templateData.templateName" fluid />
</div>
<div class="field-group">
<label for="templateDescription" class="field-label">Description</label>
<InputText id="templateDescription" v-model="templateData.description" fluid />
</div>
<div class="confirmation-buttons">
<Button label="Cancel" @click="$emit('update:visible', false)" severity="secondary" />
<Button label="Submit" @click="submit" :disabled="!templateData.templateName" />
</div>
</div>
</Modal>
</template>
<script setup>
import { reactive, watch } from "vue";
import Modal from "../common/Modal.vue";
import InputText from "primevue/inputtext";
import Button from "primevue/button";
const props = defineProps({
visible: {
type: Boolean,
required: true,
},
});
const emit = defineEmits(["update:visible", "save"]);
const templateData = reactive({
templateName: "",
description: "",
});
watch(
() => props.visible,
(newVal) => {
if (newVal) {
templateData.templateName = "";
templateData.description = "";
}
},
);
const submit = () => {
emit("save", { ...templateData });
};
</script>
<style scoped>
.field-label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
.field-group {
margin-bottom: 1rem;
}
.confirmation-buttons {
display: flex;
gap: 1rem;
justify-content: flex-end;
margin-top: 1rem;
}
.modal-content {
padding: 1rem;
}
</style>

View file

@ -59,6 +59,32 @@
</div> </div>
</div> </div>
<!-- Template Section -->
<div class="template-section">
<div v-if="isNew">
<label for="template" class="field-label">Template</label>
<Select
v-model="selectedTemplate"
:options="templates"
optionLabel="templateName"
optionValue="name"
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>
<div v-else>
<Button label="Save As Template" icon="pi pi-save" @click="openSaveTemplateModal" />
</div>
</div>
<!-- Items Section --> <!-- Items Section -->
<div class="items-section"> <div class="items-section">
<h3>Items</h3> <h3>Items</h3>
@ -177,6 +203,13 @@
<Button label="Submit" @click="submitResponse"/> <Button label="Submit" @click="submitResponse"/>
</Modal> </Modal>
<!-- Save Template Modal -->
<SaveTemplateModal
:visible="showSaveTemplateModal"
@update:visible="showSaveTemplateModal = $event"
@save="confirmSaveTemplate"
/>
<!-- Address Search Modal --> <!-- Address Search Modal -->
<Modal <Modal
:visible="showAddressModal" :visible="showAddressModal"
@ -290,6 +323,7 @@
import { ref, reactive, computed, onMounted, watch } from "vue"; import { ref, reactive, computed, onMounted, watch } from "vue";
import { useRoute, useRouter } from "vue-router"; import { useRoute, useRouter } from "vue-router";
import Modal from "../common/Modal.vue"; import Modal from "../common/Modal.vue";
import SaveTemplateModal from "../modals/SaveTemplateModal.vue";
import DataTable from "../common/DataTable.vue"; import DataTable from "../common/DataTable.vue";
import DocHistory from "../common/DocHistory.vue"; import DocHistory from "../common/DocHistory.vue";
import InputText from "primevue/inputtext"; import InputText from "primevue/inputtext";
@ -334,11 +368,14 @@ const contactOptions = ref([]);
const quotationItems = ref([]); const quotationItems = ref([]);
const selectedItems = ref([]); const selectedItems = ref([]);
const responses = ref(["Accepted", "Rejected"]); const responses = ref(["Accepted", "Rejected"]);
const templates = ref([]);
const selectedTemplate = ref(null);
const showAddressModal = ref(false); const showAddressModal = ref(false);
const showAddItemModal = ref(false); const showAddItemModal = ref(false);
const showConfirmationModal = ref(false); const showConfirmationModal = ref(false);
const showResponseModal = ref(false); const showResponseModal = ref(false);
const showSaveTemplateModal = ref(false);
const addressSearchResults = ref([]); const addressSearchResults = ref([]);
const itemSearchTerm = ref(""); const itemSearchTerm = ref("");
@ -359,6 +396,67 @@ const itemColumns = [
]; ];
// Methods // 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.name === 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 openSaveTemplateModal = () => {
showSaveTemplateModal.value = true;
};
const confirmSaveTemplate = async (templateData) => {
try {
const data = {
templateName: templateData.templateName,
description: templateData.description,
company: company.currentCompany,
sourceQuotation: estimate.value.name,
items: selectedItems.value.map(item => ({
itemCode: item.itemCode,
itemName: item.itemName,
description: item.description,
qty: item.qty,
standardRate: item.standardRate,
discountPercentage: item.discountPercentage
}))
};
await Api.createEstimateTemplate(data);
notificationStore.addSuccess("Template saved successfully", "success");
showSaveTemplateModal.value = false;
} catch (error) {
console.error("Error saving template:", error);
notificationStore.addNotification("Failed to save template", "error");
}
};
const searchAddresses = async () => { const searchAddresses = async () => {
const searchTerm = formData.address.trim(); const searchTerm = formData.address.trim();
if (!searchTerm) return; if (!searchTerm) return;
@ -574,6 +672,10 @@ watch(
}, },
); );
watch(() => company.currentCompany, () => {
fetchTemplates();
});
// Watch for query param changes to refresh page behavior // Watch for query param changes to refresh page behavior
watch( watch(
() => route.query, () => route.query,
@ -672,6 +774,10 @@ onMounted(async () => {
console.error("Error loading quotation items:", error); console.error("Error loading quotation items:", error);
} }
if (isNew.value) {
fetchTemplates();
}
if (addressQuery.value && isNew.value) { if (addressQuery.value && isNew.value) {
// Creating new estimate - pre-fill address // Creating new estimate - pre-fill address
await selectAddress(addressQuery.value); await selectAddress(addressQuery.value);
@ -747,7 +853,8 @@ onMounted(async () => {
} }
.address-section, .address-section,
.contact-section { .contact-section,
.template-section {
margin-bottom: 1.5rem; margin-bottom: 1.5rem;
} }
@ -1014,5 +1121,23 @@ onMounted(async () => {
font-size: 0.9em; font-size: 0.9em;
color: #333; color: #333;
} }
.template-option {
display: flex;
flex-direction: column;
}
.template-name {
font-weight: bold;
}
.template-desc {
font-size: 0.85rem;
color: #666;
}
.field-group {
margin-bottom: 1rem;
}
</style> </style>
<parameter name="filePath"></parameter> <parameter name="filePath"></parameter>