accounting workflows
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/SupplierReturnManager.php';
|
||||
require '../../../assets/utils/classes/WarehouseManager.php';
|
||||
|
||||
$return_id = (int)($data['id'] ?? 0);
|
||||
|
||||
if (!$return_id) {
|
||||
$answer['message'] = 'Invalid ID.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
try {
|
||||
dbTransaction($pdo2, function($pdo) use ($return_id, $company_id, $logging) {
|
||||
$mgr = new SupplierReturnManager($pdo, $company_id);
|
||||
$whMgmt = new WarehouseManager($pdo, $company_id);
|
||||
$mgr->cancelReturn($return_id, $logging, $whMgmt);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Supplier return cancelled.';
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/PurchaseOrderManager.php';
|
||||
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
if (!$id) {
|
||||
$answer['message'] = 'Invalid PO ID.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
try {
|
||||
dbTransaction($pdo2, function($pdo) use ($id, $company_id, $logging) {
|
||||
$po = new PurchaseOrderManager($pdo, $company_id);
|
||||
$po->closePo($id, $logging);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Purchase order closed. No further receipts will be accepted.';
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/SupplierReturnManager.php';
|
||||
require '../../../assets/utils/classes/WarehouseManager.php';
|
||||
require '../../../assets/utils/classes/InvoiceManager.php';
|
||||
require '../../../assets/utils/classes/CompanySettingManager.php';
|
||||
|
||||
$return_id = (int)($data['id'] ?? 0);
|
||||
|
||||
if (!$return_id) {
|
||||
$answer['message'] = 'Invalid ID.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$csm = new CompanySettingManager($pdo1, $company_id);
|
||||
$auto_approve = (int)$csm->get('default_stock_status') === 1;
|
||||
$auto_dn = (int)$csm->get('auto_invoice_and_credit_note') === 1;
|
||||
|
||||
try {
|
||||
dbTransaction($pdo2, function($pdo) use ($return_id, $company_id, $logging, $uuid, $auto_approve, $auto_dn) {
|
||||
$mgr = new SupplierReturnManager($pdo, $company_id);
|
||||
$whMgmt = new WarehouseManager($pdo, $company_id);
|
||||
$invMgmt = new InvoiceManager($pdo, $company_id);
|
||||
$mgr->confirmReturn($return_id, $uuid, $logging, $whMgmt, $auto_approve, $invMgmt, $auto_dn);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Supplier return confirmed.';
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/SupplierReturnManager.php';
|
||||
|
||||
$mgr = new SupplierReturnManager($pdo2, $company_id);
|
||||
|
||||
try {
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $action, &$answer) {
|
||||
$mgr = new SupplierReturnManager($pdo, $company_id);
|
||||
|
||||
// Tracking-only update for confirmed returns
|
||||
if (($data['_action'] ?? '') === 'update_tracking') {
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
$mgr->updateTracking($id, $data['shipping_tracking_number'] ?? '');
|
||||
$answer['success'] = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
$raw_items = $data['items'] ?? null;
|
||||
$items = is_array($raw_items)
|
||||
? $raw_items
|
||||
: (json_decode($raw_items ?? '[]', true) ?: []);
|
||||
|
||||
$payload = array_merge($data, ['items' => $items]);
|
||||
$new_id = $mgr->saveReturn($payload, $logging);
|
||||
|
||||
$answer['success'] = 1;
|
||||
if ($new_id) $answer['new_id'] = $new_id;
|
||||
});
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/InvoiceManager.php';
|
||||
|
||||
$po_id = (int)($data['po_id'] ?? 0);
|
||||
|
||||
if (!$po_id) {
|
||||
$answer['message'] = 'Invalid PO ID.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
try {
|
||||
$new_id = null;
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($po_id, $company_id, $logging, &$new_id) {
|
||||
$inv = new InvoiceManager($pdo, $company_id);
|
||||
$new_id = $inv->createFromPo($po_id, $logging);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require_role($user_role, ['owner', 'admin', 'staff']);
|
||||
require '../../../assets/utils/classes/InvoiceManager.php';
|
||||
|
||||
$return_id = (int)($data['return_id'] ?? 0);
|
||||
|
||||
if (!$return_id) {
|
||||
$answer['message'] = 'Invalid return ID.';
|
||||
http_response_code(400);
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
try {
|
||||
$new_id = null;
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($return_id, $company_id, $logging, &$new_id) {
|
||||
$inv = new InvoiceManager($pdo, $company_id);
|
||||
$new_id = $inv->createFromSupplierReturn($return_id, $logging);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['new_id'] = $new_id;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer['message'] = 'Database error, please try again.';
|
||||
http_response_code(500);
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -16,23 +16,13 @@
|
||||
}
|
||||
$answer['output'] = $result;
|
||||
|
||||
// Calculate receivable items — PO items minus already received qty
|
||||
$po_items = $result['items'] ?? [];
|
||||
$receivable = [];
|
||||
foreach ($po_items as $i => $item) {
|
||||
$ordered = (float)($item['quantity'] ?? 0);
|
||||
$received = (float)($item['received_qty'] ?? 0);
|
||||
$remaining = $ordered - $received;
|
||||
if ($remaining > 0) {
|
||||
$receivable[] = array_merge($item, [
|
||||
'item_id' => (int)($item['item_id'] ?? $i),
|
||||
'ordered_qty' => $ordered,
|
||||
'received_qty' => $received,
|
||||
'remaining_qty' => $remaining,
|
||||
]);
|
||||
}
|
||||
}
|
||||
$answer['receivable_items'] = $receivable;
|
||||
$answer['receivable_items'] = $po->getReceivableItems($result);
|
||||
$answer['returnable_items'] = $po->getReturnableItems($result);
|
||||
|
||||
// Check for active purchase invoice linked to this PO
|
||||
$active_pi_id = $po->getActivePurchaseInvoiceId($id);
|
||||
$answer['has_active_pi'] = $active_pi_id > 0 ? 1 : 0;
|
||||
$answer['active_pi_id'] = $active_pi_id;
|
||||
|
||||
} else {
|
||||
$answer['output'] = $po->getPoList();
|
||||
@@ -40,4 +30,4 @@
|
||||
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
?>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/SupplierReturnManager.php';
|
||||
|
||||
$mgr = new SupplierReturnManager($pdo2, $company_id);
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
|
||||
if ($id > 0) {
|
||||
$result = $mgr->getReturnById($id);
|
||||
if (!$result) {
|
||||
http_response_code(404);
|
||||
$answer['message'] = 'Supplier return not found.';
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
|
||||
$active_dn_id = $mgr->getActiveDebitNoteId($id);
|
||||
$result['has_active_dn'] = $active_dn_id > 0 ? 1 : 0;
|
||||
$result['active_dn_id'] = $active_dn_id;
|
||||
|
||||
$answer['output'] = $result;
|
||||
} else {
|
||||
$po_id = (int)($data['po_id'] ?? 0);
|
||||
$answer['output'] = $mgr->getReturnList($po_id);
|
||||
}
|
||||
|
||||
$answer['success'] = 1;
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
Reference in New Issue
Block a user