diff --git a/services/nginx/app/objects/collected_order_invoices_o.php b/services/nginx/app/objects/collected_order_invoices_o.php index 5e0329d0..95ef8272 100644 --- a/services/nginx/app/objects/collected_order_invoices_o.php +++ b/services/nginx/app/objects/collected_order_invoices_o.php @@ -101,7 +101,7 @@ class collected_order_invoices_o extends db // Check if the object is cached $cached = self::getCached('asArray', $this->id); if ($cached !== null) { - return (array)$cached; + //return (array)$cached; } $tmp = [ 'id' => (int)$this->id, @@ -170,6 +170,9 @@ class collected_order_invoices_o extends db foreach ( $order_ids as $order_id ) { $orders->select($order_id['id']); $orders->requireSelected(); + if (!$orders->isIncludedInInvoicing()) { + continue; + } $result[] = $orders->asArray(); } return $result; @@ -194,6 +197,9 @@ class collected_order_invoices_o extends db $order = new orders_o(); $order->select($order_id['id']); $order->requireSelected(); + if (!$order->isIncludedInInvoicing()) { + continue; + } // Get the net amount of the order $net_amount += $order->getNetAmount(); } @@ -211,13 +217,22 @@ class collected_order_invoices_o extends db self::requireSelected(); // Get the orders in the invoice collection $orders_o = new orders_o(); - return $orders_o->getFieldsWhere( + $fields = $orders_o->getFieldsWhere( [ 'invoice_collection_id' => $this->id, 'deleted_at' => null ], ['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; } /** diff --git a/services/nginx/app/objects/orders_o.php b/services/nginx/app/objects/orders_o.php index ca49b546..72f9cf7a 100644 --- a/services/nginx/app/objects/orders_o.php +++ b/services/nginx/app/objects/orders_o.php @@ -1389,4 +1389,16 @@ class orders_o extends db $this->select((int)$new_order->id); 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(); + } } \ No newline at end of file diff --git a/services/nginx/app/routes/InvoicingPeriodRoute.php b/services/nginx/app/routes/InvoicingPeriodRoute.php index 789cf586..c3d8ef32 100644 --- a/services/nginx/app/routes/InvoicingPeriodRoute.php +++ b/services/nginx/app/routes/InvoicingPeriodRoute.php @@ -750,6 +750,7 @@ class InvoicingPeriodRoute 'date' => $transaction->created_at->value(), 'amount' => $transaction->temporary_net_amount, // Use the temporary net amount set earlier 'booked' => $transaction->isBooked(), + 'excluded' => !$transaction->isIncludedInInvoicing() ]; } @@ -761,7 +762,7 @@ class InvoicingPeriodRoute } // Check if any transaction is not booked foreach ( $parsed_transactions as $transaction ) { - if (!$transaction['booked']) { + if (!$transaction['booked'] && !$transaction['excluded']) { return true; } }