resultQuery( "SELECT service_id FROM OGP_DB_PREFIXbilling_services WHERE home_cfg_id = " . intval($home_cfg_id) . " AND enabled = 1 LIMIT 1" ); if (!empty($services[0]['service_id'])) { $service_id = intval($services[0]['service_id']); } // ------------------------------------------------------------------ // // 2. Resolve owner's name & email for the invoice record. // // ------------------------------------------------------------------ // $customer_name = ''; $customer_email = ''; $user_row = $db->getUserById(intval($user_id)); if (!empty($user_row)) { $customer_name = trim( ($user_row['users_fname'] ?? '') . ' ' . ($user_row['users_lname'] ?? '') ); $customer_email = $user_row['users_email'] ?? ''; } $now = date('Y-m-d H:i:s'); $end_date = date('Y-m-d H:i:s', strtotime('+31 days')); $ftp_flag = $ftp ? 'enabled' : 'disabled'; // ------------------------------------------------------------------ // // 3. Insert billing_invoice (amount=0, payment_status='paid'). // // Note: billing_invoices.status is the invoice payment lifecycle // // ('paid'/'unpaid'/'due') — distinct from billing_orders.status // // which uses the three-value order lifecycle (Active/Invoiced/ // // Expired). // // ------------------------------------------------------------------ // $invoice_fields = array( 'order_id' => 0, 'user_id' => intval($user_id), 'service_id' => $service_id, 'home_id' => intval($home_id), 'home_name' => $home_name, 'ip' => intval($rserver_id), 'max_players' => intval($max_players), 'remote_control_password' => $remote_control_password, 'ftp_password' => $ftp_password, 'customer_name' => $customer_name, 'customer_email' => $customer_email, 'amount' => '0.00', 'discount_amount' => '0.00', 'currency' => 'USD', 'status' => 'paid', 'billing_status' => 'Active', 'invoice_date' => $now, 'due_date' => $now, 'paid_date' => $now, 'payment_txid' => 'admin-created', 'payment_method' => 'admin', 'description' => 'Admin-created server: ' . $home_name, 'invoice_duration' => 'month', 'rate_type' => 'monthly', 'rate_per_player' => '0.0000', 'players' => intval($max_players), 'period_start' => $now, 'period_end' => $end_date, 'subtotal' => '0.00', 'total_due' => '0.00', 'payment_status' => 'paid', 'qty' => 1, ); $invoice_id = $db->resultInsertId('billing_invoices', $invoice_fields); if ($invoice_id === FALSE) { return FALSE; } // ------------------------------------------------------------------ // // 4. Insert billing_order (status='Active', already provisioned). // // ------------------------------------------------------------------ // $order_fields = array( 'user_id' => intval($user_id), 'service_id' => $service_id, 'home_name' => $home_name, 'ip' => intval($rserver_id), 'qty' => 1, 'invoice_duration' => 'month', 'max_players' => intval($max_players), 'price' => '0.00', 'discount_amount' => '0.00', 'remote_control_password' => $remote_control_password, 'ftp_password' => $ftp_password, 'home_id' => intval($home_id), 'status' => 'Active', 'order_date' => $now, 'end_date' => $end_date, 'payment_txid' => 'admin-created', 'paid_ts' => $now, 'coupon_id' => 0, ); $order_id = $db->resultInsertId('billing_orders', $order_fields); if ($order_id === FALSE) { return FALSE; } // ------------------------------------------------------------------ // // 5. Link the invoice back to the new order. // // ------------------------------------------------------------------ // $db->query( "UPDATE OGP_DB_PREFIXbilling_invoices SET order_id = " . intval($order_id) . " WHERE invoice_id = " . intval($invoice_id) ); return $order_id; } }