2025-08-18 22:05:33 +03:30
|
|
|
/**
|
|
|
|
|
* Utility functions for document approval management
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if current user can approve a specific document type
|
|
|
|
|
* @param {Object} businessSettings - Business settings object
|
|
|
|
|
* @param {number} currentUserId - Current user ID
|
|
|
|
|
* @param {boolean} isBusinessOwner - Whether current user is business owner
|
|
|
|
|
* @param {string} documentType - Type of document (invoice, warehouse, financial)
|
|
|
|
|
* @returns {boolean} - Whether user can approve
|
|
|
|
|
*/
|
|
|
|
|
export function canApproveDocument(businessSettings, currentUserEmail, isBusinessOwner, documentType) {
|
|
|
|
|
// If two-step approval is not enabled, anyone can approve
|
|
|
|
|
if (!businessSettings.requireTwoStepApproval) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Business owner can always approve
|
|
|
|
|
if (isBusinessOwner) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check specific approver based on document type
|
|
|
|
|
switch (documentType) {
|
|
|
|
|
case 'invoice':
|
|
|
|
|
return businessSettings.invoiceApprover === currentUserEmail;
|
|
|
|
|
case 'warehouse':
|
|
|
|
|
return businessSettings.warehouseApprover === currentUserEmail;
|
|
|
|
|
case 'financial':
|
|
|
|
|
return businessSettings.financialApprover === currentUserEmail;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get document type from document object
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @returns {string} - Document type
|
|
|
|
|
*/
|
|
|
|
|
export function getDocumentType(document) {
|
|
|
|
|
if (document.type) {
|
|
|
|
|
return document.type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback based on document properties
|
|
|
|
|
if (document.invoiceNumber) {
|
|
|
|
|
return 'invoice';
|
|
|
|
|
}
|
|
|
|
|
if (document.warehouseNumber) {
|
|
|
|
|
return 'warehouse';
|
|
|
|
|
}
|
|
|
|
|
if (document.paymentType || document.receiptType) {
|
|
|
|
|
return 'financial';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'unknown';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if document needs approval
|
|
|
|
|
* @param {Object} businessSettings - Business settings object
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @returns {boolean} - Whether document needs approval
|
|
|
|
|
*/
|
|
|
|
|
export function needsApproval(businessSettings, document) {
|
|
|
|
|
if (!businessSettings.requireTwoStepApproval) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if document is pending approval
|
|
|
|
|
return document.status === 'pending_approval' ||
|
|
|
|
|
document.approvalStatus === 'pending' ||
|
|
|
|
|
document.approved === false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get approval button visibility
|
|
|
|
|
* @param {Object} businessSettings - Business settings object
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @param {number} currentUserId - Current user ID
|
|
|
|
|
* @param {boolean} isBusinessOwner - Whether current user is business owner
|
|
|
|
|
* @returns {boolean} - Whether approval button should be visible
|
|
|
|
|
*/
|
|
|
|
|
export function shouldShowApprovalButton(businessSettings, document, currentUserEmail, isBusinessOwner) {
|
|
|
|
|
// Check if document needs approval
|
|
|
|
|
if (!needsApproval(businessSettings, document)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if user can approve
|
|
|
|
|
const documentType = getDocumentType(document);
|
|
|
|
|
return canApproveDocument(businessSettings, currentUserEmail, isBusinessOwner, documentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get approval button text
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @returns {string} - Button text
|
|
|
|
|
*/
|
|
|
|
|
export function getApprovalButtonText(document) {
|
|
|
|
|
if (document.status === 'pending_approval' || document.approvalStatus === 'pending') {
|
|
|
|
|
return 'تایید سند';
|
|
|
|
|
}
|
|
|
|
|
if (document.approved === false) {
|
|
|
|
|
return 'تایید مجدد';
|
|
|
|
|
}
|
|
|
|
|
return 'تایید';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get approval status text
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @returns {string} - Status text
|
|
|
|
|
*/
|
|
|
|
|
export function getApprovalStatusText(document) {
|
|
|
|
|
if (document.status === 'pending_approval' || document.approvalStatus === 'pending') {
|
|
|
|
|
return 'در انتظار تایید';
|
|
|
|
|
}
|
|
|
|
|
if (document.approved === true) {
|
|
|
|
|
return 'تایید شده';
|
|
|
|
|
}
|
|
|
|
|
if (document.approved === false) {
|
|
|
|
|
return 'رد شده';
|
|
|
|
|
}
|
2025-08-19 00:39:07 +03:30
|
|
|
return 'تایید شده';
|
2025-08-18 22:05:33 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get approval status color
|
|
|
|
|
* @param {Object} document - Document object
|
|
|
|
|
* @returns {string} - Status color
|
|
|
|
|
*/
|
|
|
|
|
export function getApprovalStatusColor(document) {
|
|
|
|
|
if (document.status === 'pending_approval' || document.approvalStatus === 'pending') {
|
|
|
|
|
return 'warning';
|
|
|
|
|
}
|
|
|
|
|
if (document.approved === true) {
|
|
|
|
|
return 'success';
|
|
|
|
|
}
|
|
|
|
|
if (document.approved === false) {
|
|
|
|
|
return 'error';
|
|
|
|
|
}
|
2025-08-19 00:39:07 +03:30
|
|
|
return 'success';
|
2025-08-18 22:05:33 +03:30
|
|
|
}
|