diff --git a/services/nginx/app/objects/collected_order_invoices_o.php b/services/nginx/app/objects/collected_order_invoices_o.php index d8bbf29c..02736df6 100644 --- a/services/nginx/app/objects/collected_order_invoices_o.php +++ b/services/nginx/app/objects/collected_order_invoices_o.php @@ -6,7 +6,13 @@ use classes\db; use classes\economic; use classes\object_property; use classes\stripe; +use config\economic_admin_fee_monthly_c; +use config\economic_admin_fee_order_c; +use config\economic_fee_product_id_c; use Exception; +use objects\order_items_o; +use objects\orders_o; +use objects\users_o; use Stripe\Exception\ApiErrorException; use traits\db_object_t; @@ -422,6 +428,9 @@ class collected_order_invoices_o extends db self::requireOpen(); } + // Ensure the administration fee is added to the invoice collection + self::ensureAdministrationFee(); + // Create an economic draft self::createInvoiceDraft(); @@ -680,6 +689,88 @@ class collected_order_invoices_o extends db } } + /** + * Ensure that the administration fee is added to the invoice collection, if it has not already been paid this month. + * @return void + * @throws Exception + */ + public function ensureAdministrationFee(): void + { + // Require the invoice collection to be selected + self::requireSelected(); + // Get the user object + $user = (new users_o())->getUserByCustomerNumber($this->customer_number->value()); + // Check if the user has already paid the administration fee this month + if (self::hasPaidAdministrationFeeThisMonth()) { + return; + } + + // Get the fee product ID + $fee_product_id = (int)(new economic_fee_product_id_c())->getVariableValue(); + // Get the fee price + if ($user->invoicePerOrder()) { + $price = (int)(new economic_admin_fee_order_c())->getVariableValue(); + } else { + $price = (int)(new economic_admin_fee_monthly_c())->getVariableValue(); + } + + // Add the fee order + $order = (new orders_o())->add( + (int)$user->id, + 1857, + 'Administrations og miljøtillæg', + '', + 10, + '', + '', + '' + ); + // Assign the order to the invoice collection + $order->assignToInvoiceCollection($this->id); + // Add the order item + (new order_items_o())->addItemToOrder( + (int)$order->id, + $fee_product_id, + 1857, + 1, + null, + '', + $price + ); + } + + /** + * Check if the customer has already paid the administration fee this month + * @return bool + * @throws Exception + */ + public function hasPaidAdministrationFeeThisMonth(): bool + { + global $db; + // Require the invoice collection to be selected + self::requireSelected(); + // Get the customer number + $customer_number = (int)$this->customer_number->value(); + // Get the fee product ID + $fee_product_id = (int)(new economic_fee_product_id_c())->getVariableValue(); + // Get the first and last day of the month + $first_day_of_month = date('Y-m-01 00:00:00'); + $last_day_of_month = date('Y-m-t 23:59:59'); + // Check if the customer has already paid the fee this month + $sql = "SELECT COUNT(*) as count + FROM order_items oi + JOIN orders o ON oi.order_id = o.id + JOIN users u ON o.customer_id = u.id + WHERE u.customer_number = $customer_number + AND oi.product_id = $fee_product_id + AND o.created_at BETWEEN '$first_day_of_month' AND '$last_day_of_month' + AND o.deleted_at IS NULL"; + // Execute the query + $result = $db->query($sql); + $row = $result->fetch_assoc(); + return (int)$row['count'] > 0; + } + /** * Require the invoice collection to not be booked * @throws Exception If the request was not successful