Add isIncludedInInvoicing method and implement invoicing exclusions

- Introduced `isIncludedInInvoicing` in `orders_o` to check department-based invoicing exclusions.
- Updated routes and methods to skip orders excluded from invoicing.
- Refined transaction and order handling to respect invoicing settings, ensuring correct filtering.
This commit is contained in:
Jeppe Bundgaard
2025-11-25 14:39:49 +01:00
parent 537582e44d
commit 3dba2dedac
3 changed files with 31 additions and 3 deletions
@@ -101,7 +101,7 @@ class collected_order_invoices_o extends db
// Check if the object is cached // Check if the object is cached
$cached = self::getCached('asArray', $this->id); $cached = self::getCached('asArray', $this->id);
if ($cached !== null) { if ($cached !== null) {
return (array)$cached; //return (array)$cached;
} }
$tmp = [ $tmp = [
'id' => (int)$this->id, 'id' => (int)$this->id,
@@ -170,6 +170,9 @@ class collected_order_invoices_o extends db
foreach ( $order_ids as $order_id ) { foreach ( $order_ids as $order_id ) {
$orders->select($order_id['id']); $orders->select($order_id['id']);
$orders->requireSelected(); $orders->requireSelected();
if (!$orders->isIncludedInInvoicing()) {
continue;
}
$result[] = $orders->asArray(); $result[] = $orders->asArray();
} }
return $result; return $result;
@@ -194,6 +197,9 @@ class collected_order_invoices_o extends db
$order = new orders_o(); $order = new orders_o();
$order->select($order_id['id']); $order->select($order_id['id']);
$order->requireSelected(); $order->requireSelected();
if (!$order->isIncludedInInvoicing()) {
continue;
}
// Get the net amount of the order // Get the net amount of the order
$net_amount += $order->getNetAmount(); $net_amount += $order->getNetAmount();
} }
@@ -211,13 +217,22 @@ class collected_order_invoices_o extends db
self::requireSelected(); self::requireSelected();
// Get the orders in the invoice collection // Get the orders in the invoice collection
$orders_o = new orders_o(); $orders_o = new orders_o();
return $orders_o->getFieldsWhere( $fields = $orders_o->getFieldsWhere(
[ [
'invoice_collection_id' => $this->id, 'invoice_collection_id' => $this->id,
'deleted_at' => null 'deleted_at' => null
], ],
['id'] ['id']
); );
// Remove all orders that are not included in invoicing
foreach ( $fields as $key => $order ) {
$orders_o->select($order['id']);
$orders_o->requireSelected();
if (!$orders_o->isIncludedInInvoicing()) {
unset($fields[$key]);
}
}
return $fields;
} }
/** /**
+12
View File
@@ -1389,4 +1389,16 @@ class orders_o extends db
$this->select((int)$new_order->id); $this->select((int)$new_order->id);
return $this; return $this;
} }
/**
* If the transaction should be included in invoicing, based on the department.
* Some departments have a setting to exclude them from invoicing.
* @return bool
* @throws Exception If the order is not selected
*/
public function isIncludedInInvoicing(): bool
{
self::requireSelected();
return !(new departments_o())->select((int)$this->department_id->value())->isExcludedFromInvoicing();
}
} }
@@ -750,6 +750,7 @@ class InvoicingPeriodRoute
'date' => $transaction->created_at->value(), 'date' => $transaction->created_at->value(),
'amount' => $transaction->temporary_net_amount, // Use the temporary net amount set earlier 'amount' => $transaction->temporary_net_amount, // Use the temporary net amount set earlier
'booked' => $transaction->isBooked(), 'booked' => $transaction->isBooked(),
'excluded' => !$transaction->isIncludedInInvoicing()
]; ];
} }
@@ -761,7 +762,7 @@ class InvoicingPeriodRoute
} }
// Check if any transaction is not booked // Check if any transaction is not booked
foreach ( $parsed_transactions as $transaction ) { foreach ( $parsed_transactions as $transaction ) {
if (!$transaction['booked']) { if (!$transaction['booked'] && !$transaction['excluded']) {
return true; return true;
} }
} }