예제 #1
0
 function load_data()
 {
     log_debug("name_server", "Executing load_data()");
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT * FROM name_servers WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         // set attributes
         $this->data = $sql_obj->data[0];
         // fetch sync statuses
         if ($this->data["server_type"] == "api") {
             if (sql_get_singlevalue("SELECT value FROM config WHERE name='SYNC_STATUS_CONFIG'") != $sql_obj->data[0]["api_sync_config"]) {
                 // out of sync, set to date
                 $this->data["sync_status_config"] = $sql_obj->data[0]["api_sync_config"];
             }
             if (time() - $sql_obj->data[0]["api_sync_log"] > 86400) {
                 // logging hasn't happened for at least 24 hours, flag logging as failed
                 $this->data["sync_status_log"] = $sql_obj->data[0]["api_sync_log"];
             }
         } else {
             $this->data["sync_status_config"] = time();
             $this->data["sync_status_log"] = time();
         }
         return 1;
     }
     // failure
     return 0;
 }
예제 #2
0
 function list_taxes()
 {
     log_debug("accounts_taxes_manage_soap", "Executing list_taxes()");
     if (user_permissions_get("accounts_taxes_view")) {
         // fetch taxes
         $sql_obj = new sql_query();
         $sql_obj->string = "SELECT id, name_tax, taxrate, chartid, taxnumber, description FROM account_taxes";
         $sql_obj->execute();
         $return = NULL;
         if ($sql_obj->num_rows()) {
             $sql_obj->fetch_array();
             // package data into array for passing back to SOAP client
             foreach ($sql_obj->data as $data) {
                 $return_tmp = NULL;
                 $return_tmp["id"] = $data["id"];
                 $return_tmp["name_tax"] = $data["name_tax"];
                 $return_tmp["taxrate"] = $data["taxrate"];
                 $return_tmp["chartid"] = $data["chartid"];
                 $return_tmp["chartid_label"] = sql_get_singlevalue("SELECT CONCAT_WS('--', code_chart, description) as value FROM account_charts WHERE id='" . $data["chartid"] . "'");
                 $return_tmp["taxnumber"] = $data["taxnumber"];
                 $return_tmp["description"] = $data["description"];
                 $return[] = $return_tmp;
             }
         }
         return $return;
     } else {
         throw new SoapFault("Sender", "ACCESS_DENIED");
     }
 }
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "users_permissions_staff";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "user/user-staffaccess-edit-process.php";
     $this->obj_form->method = "post";
     // run through all the avaliable permissions
     $sql_perms_obj = new sql_query();
     $sql_perms_obj->string = "SELECT * FROM `permissions_staff`";
     $sql_perms_obj->execute();
     if ($sql_perms_obj->num_rows()) {
         $sql_perms_obj->fetch_array();
         foreach ($sql_perms_obj->data as $data_perms) {
             // define the checkbox
             $structure = NULL;
             $structure["fieldname"] = $data_perms["value"];
             $structure["type"] = "checkbox";
             $structure["options"]["label"] = $data_perms["description"];
             // check the database to see if this checkbox is selected
             $sql_obj = new sql_query();
             $sql_obj->string = "SELECT " . "id " . "FROM `users_permissions_staff` " . "WHERE " . "userid='" . $this->id . "' " . "AND permid='" . $data_perms["id"] . "' " . "AND staffid='" . $this->staffid . "'";
             $sql_obj->execute();
             if ($sql_obj->num_rows()) {
                 $structure["defaultvalue"] = "on";
             }
             // add checkbox
             $this->obj_form->add_input($structure);
             // add checkbox to subforms
             $this->obj_form->subforms["user_permissions_staff"][] = $data_perms["value"];
         }
     }
     // hidden fields
     $structure = NULL;
     $structure["fieldname"] = "id_user";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "id_staff";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->staffid;
     $this->obj_form->add_input($structure);
     // submit section
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Save Changes";
     $this->obj_form->add_input($structure);
     // define subforms
     $this->obj_form->subforms["hidden"] = array("id_user", "id_staff");
     $this->obj_form->subforms["submit"] = array("submit");
     /*
     	Note: We don't load from error data, since there should never
     	be any errors when using this form.
     */
 }
예제 #4
0
 function check_requirements()
 {
     // verify that the service exists
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id, id_rate_table FROM services WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         // verify the rate is valid
         if ($sql_obj->data[0]["id_rate_table"]) {
             $this->obj_cdr_rate_table->id = $sql_obj->data[0]["id_rate_table"];
             if (!$this->obj_cdr_rate_table->verify_id()) {
                 log_write("error", "page_output", "The requested CDR rate table is invalid, there may be some problems with the information in the database.");
                 return 0;
             }
         } else {
             log_write("error", "page_output", "You have yet to set a CDR Rate Table for this service to use - please do so using the plan page before attempting to override the rates");
             return 0;
         }
     } else {
         log_write("error", "page_output", "The requested service (" . $this->id . ") does not exist - possibly the service has been deleted.");
         return 0;
     }
     unset($sql_obj);
     // verify that this is a phone service
     if ($this->service_type != ("phone_single" || "phone_trunk" || "phone_tollfree")) {
         log_write("error", "page_output", "The requested service is not a phone service.");
         return 0;
     }
     return 1;
 }
 function check_requirements()
 {
     // verify that the invoice exists
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id FROM account_ar WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if (!$sql_obj->num_rows()) {
         log_write("error", "page_output", "The requested invoice (" . $this->id . ") does not exist - possibly the invoice has been deleted.");
         return 0;
     }
     unset($sql_obj);
     // verify that the item id supplied exists and fetch required information
     if ($this->itemid) {
         $sql_obj = new sql_query();
         $sql_obj->string = "SELECT id, type FROM account_items WHERE id='" . $this->itemid . "' AND invoiceid='" . $this->id . "' LIMIT 1";
         $sql_obj->execute();
         if (!$sql_obj->num_rows()) {
             log_write("error", "page_output", "The requested payment/invoice combination does not exist. Are you trying to use a link to a deleted payment?");
             return 0;
         } else {
             $sql_obj->fetch_array();
             $this->item_type = $sql_obj->data[0]["type"];
         }
     }
     return 1;
 }
예제 #6
0
 function check_requirements()
 {
     // verify that project exists
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id FROM projects WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if (!$sql_obj->num_rows()) {
         log_write("error", "page_output", "The requested project (" . $this->id . ") does not exist - possibly the project has been deleted.");
         return 0;
     }
     unset($sql_obj);
     // verify that phase exists and belongs to this project
     if ($this->phaseid) {
         $sql_obj = new sql_query();
         $sql_obj->string = "SELECT projectid FROM project_phases WHERE id='" . $this->phaseid . "' LIMIT 1";
         $sql_obj->execute();
         if (!$sql_obj->num_rows()) {
             log_write("error", "page_output", "The requested phase (" . $this->phaseid . ") does not exist - possibly the phase has been deleted.");
             return 0;
         } else {
             $sql_obj->fetch_array();
             if ($sql_obj->data[0]["projectid"] != $this->id) {
                 log_write("error", "page_output", "The requested phase (" . $this->phaseid . ") does not belong to the selected project (" . $this->id . ")");
                 return 0;
             }
         }
         unset($sql_obj);
     }
     return 1;
 }
예제 #7
0
function quotes_render_summarybox($id)
{
    log_debug("inc_quotes", "quotes_render_summarybox({$id})");
    // fetch quote information
    $sql_obj = new sql_query();
    $sql_obj->string = "SELECT code_quote, amount_total, date_validtill, date_sent, sentmethod FROM account_quotes WHERE id='{$id}' LIMIT 1";
    $sql_obj->execute();
    if ($sql_obj->num_rows()) {
        $sql_obj->fetch_array();
        if ($sql_obj->data[0]["amount_total"] == 0) {
            print "<table width=\"100%\" class=\"table_highlight_important\">";
            print "<tr>";
            print "<td>";
            print "<b>Quote " . $sql_obj->data[0]["code_quote"] . " has no items on it</b>";
            print "<p>This quote needs to have some items added to it using the links in the nav menu above.</p>";
            print "</td>";
            print "</tr>";
            print "</table>";
        } else {
            if (time_date_to_timestamp($sql_obj->data[0]["date_validtill"]) <= time()) {
                print "<table width=\"100%\" class=\"table_highlight_important\">";
                print "<tr>";
                print "<td>";
                print "<p><b>Quote " . $sql_obj->data[0]["code_quote"] . " has now expired and is no longer valid.</b></p>";
                print "</td>";
                print "</tr>";
                print "</table>";
            } else {
                print "<table width=\"100%\" class=\"table_highlight_important\">";
                print "<tr>";
                print "<td>";
                print "<b>Quote " . $sql_obj->data[0]["code_quote"] . " is currently valid.</b>";
                print "<table cellpadding=\"4\">";
                print "<tr>";
                print "<td>Quote Total:</td>";
                print "<td>" . format_money($sql_obj->data[0]["amount_total"]) . "</td>";
                print "</tr>";
                print "<tr>";
                print "<td>Valid Until:</td>";
                print "<td>" . $sql_obj->data[0]["date_validtill"] . "</td>";
                print "</tr>";
                print "<tr>";
                print "<td>Date Sent:</td>";
                if ($sql_obj->data[0]["sentmethod"] == "") {
                    print "<td><i>Has not been sent to customer</i></td>";
                } else {
                    print "<td>" . $sql_obj->data[0]["date_sent"] . " (" . $sql_obj->data[0]["sentmethod"] . ")</td>";
                }
                print "</tr>";
                print "</tr></table>";
                print "</td>";
                print "</tr>";
                print "</table>";
            }
        }
        print "<br>";
    }
}
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "users_permissions_staff";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "user/user-staffaccess-edit-process.php";
     $this->obj_form->method = "post";
     // staff member dropdown
     $structure = form_helper_prepare_dropdownfromdb("id_staff", "SELECT id, staff_code as label, name_staff as label1 FROM `staff` ORDER BY name_staff");
     $this->obj_form->add_input($structure);
     $this->obj_form->subforms["user_permissions_selectstaff"] = array("id_staff");
     /*
     	Permissions sub-form
     */
     // run through all the avaliable permissions
     $sql_perms_obj = new sql_query();
     $sql_perms_obj->string = "SELECT * FROM `permissions_staff`";
     $sql_perms_obj->execute();
     if ($sql_perms_obj->num_rows()) {
         $sql_perms_obj->fetch_array();
         foreach ($sql_perms_obj->data as $data_perms) {
             // define the checkbox
             $structure = NULL;
             $structure["fieldname"] = $data_perms["value"];
             $structure["type"] = "checkbox";
             $structure["options"]["label"] = $data_perms["description"];
             // add checkbox
             $this->obj_form->add_input($structure);
             // add checkbox to subforms
             $this->obj_form->subforms["user_permissions_staff"][] = $data_perms["value"];
         }
     }
     // hidden fields
     $structure = NULL;
     $structure["fieldname"] = "id_user";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     // submit section
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Save Changes";
     $this->obj_form->add_input($structure);
     // define subforms
     $this->obj_form->subforms["hidden"] = array("id_user");
     $this->obj_form->subforms["submit"] = array("submit");
     /*
     	Note: We don't load from error data, since there should never
     	be any errors when using this form.
     */
 }
예제 #9
0
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "user_permissions";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "user/user-permissions-process.php";
     $this->obj_form->method = "post";
     $sql_perms_obj = new sql_query();
     $sql_perms_obj->string = "SELECT * FROM `permissions` ORDER BY value='disabled' DESC, value='admin' DESC, value";
     $sql_perms_obj->execute();
     $sql_perms_obj->fetch_array();
     foreach ($sql_perms_obj->data as $data_perms) {
         // define the checkbox
         $structure = NULL;
         $structure["fieldname"] = $data_perms["value"];
         $structure["type"] = "checkbox";
         $structure["options"]["label"] = $data_perms["description"];
         $structure["options"]["no_translate_fieldname"] = "yes";
         // check if the user has this permission
         $sql_obj = new sql_query();
         $sql_obj->string = "SELECT id FROM `users_permissions` WHERE userid='" . $this->id . "' AND permid='" . $data_perms["id"] . "'";
         $sql_obj->execute();
         if ($sql_obj->num_rows()) {
             $structure["defaultvalue"] = "on";
         }
         // add checkbox
         $this->obj_form->add_input($structure);
         // add checkbox to subforms
         $this->obj_form->subforms["user_permissions"][] = $data_perms["value"];
     }
     // user ID (hidden field)
     $structure = NULL;
     $structure["fieldname"] = "id_user";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     // submit section
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Save Changes";
     $this->obj_form->add_input($structure);
     // define subforms
     $this->obj_form->subforms["hidden"] = array("id_user");
     $this->obj_form->subforms["submit"] = array("submit");
     /*
     	Note: We don't load from error data, since there should never
     	be any errors when using this form.
     */
 }
 function execute()
 {
     /*
     	Fetch period data
     */
     $sql_period_obj = new sql_query();
     $sql_period_obj->string = "SELECT date_start, date_end FROM services_customers_periods WHERE id='" . $this->id_service_period . "' LIMIT 1";
     $sql_period_obj->execute();
     $sql_period_obj->fetch_array();
     /*
     	Fetch call charges for this period into table.
     */
     // establish a new table object
     $this->obj_table = new table();
     $this->obj_table->language = $_SESSION["user"]["lang"];
     $this->obj_table->tablename = "service_history_cdr";
     // define all the columns and structure
     $this->obj_table->add_column("date", "date", "");
     $this->obj_table->add_column("standard", "rate_billgroup", "cdr_rate_billgroups.billgroup_name");
     $this->obj_table->add_column("standard", "number_src", "usage1");
     $this->obj_table->add_column("standard", "number_dst", "usage2");
     $this->obj_table->add_column("standard", "billable_seconds", "usage3");
     $this->obj_table->add_column("money_float", "price", "");
     // defaults
     $this->obj_table->columns = array("date", "rate_billgroup", "number_src", "number_dst", "billable_seconds", "price");
     $this->obj_table->columns_order = array("date", "rate_billgroup", "number_src", "number_dst");
     // totals
     $this->obj_table->total_columns = array("billable_seconds", "price");
     // define SQL structure
     $this->obj_table->sql_obj->prepare_sql_settable("service_usage_records");
     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN cdr_rate_billgroups ON cdr_rate_billgroups.id = service_usage_records.billgroup");
     $this->obj_table->sql_obj->prepare_sql_addfield("id", "service_usage_records.id");
     $this->obj_table->sql_obj->prepare_sql_addwhere("id_service_customer = '" . $this->obj_customer->id_service_customer . "'");
     $this->obj_table->sql_obj->prepare_sql_addwhere("date >= '" . $sql_period_obj->data[0]["date_start"] . "'");
     $this->obj_table->sql_obj->prepare_sql_addwhere("date <= '" . $sql_period_obj->data[0]["date_end"] . "'");
     // acceptable filter options
     $structure = NULL;
     $structure["fieldname"] = "searchbox";
     $structure["type"] = "input";
     $structure["sql"] = "(number_src LIKE '%value%' OR number_dst LIKE '%value%')";
     $this->obj_table->add_filter($structure);
     $this->obj_table->add_fixed_option("id_customer", $this->obj_customer->id);
     $this->obj_table->add_fixed_option("id_service_customer", $this->obj_customer->id_service_customer);
     $this->obj_table->add_fixed_option("id_service_period", $this->id_service_period);
     // load settings from options form
     $this->obj_table->load_options_form();
     // run SQL query
     $this->obj_table->generate_sql();
     $this->obj_table->load_data_sql();
 }
예제 #11
0
 function check_requirements()
 {
     // verify that project exists
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id, name_project FROM projects WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if (!$sql_obj->num_rows()) {
         log_write("error", "page_output", "The requested project (" . $this->id . ") does not exist - possibly the project has been deleted.");
         return 0;
     } else {
         $sql_obj->fetch_array();
         $this->name_project = $sql_obj->data[0]["name_project"];
     }
     unset($sql_obj);
     return 1;
 }
예제 #12
0
 function check_requirements()
 {
     // verify that the account exists
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id, locked FROM account_gl WHERE id='" . $this->id . "' LIMIT 1";
     $sql_obj->execute();
     if (!$sql_obj->num_rows()) {
         log_write("error", "page_output", "The requested transaction (" . $this->id . ") does not exist - possibly the transaction has been deleted.");
         return 0;
     } else {
         $sql_obj->fetch_array();
         $this->locked = $sql_obj->data[0]["locked"];
     }
     unset($sql_obj);
     return 1;
 }
 function execute()
 {
     /*
     	Fetch period data
     */
     $sql_period_obj = new sql_query();
     $sql_period_obj->string = "SELECT date_start, date_end FROM services_customers_periods WHERE id='" . $this->id_service_period . "' LIMIT 1";
     $sql_period_obj->execute();
     $sql_period_obj->fetch_array();
     /*
     	Generate CSV CDR Output
     */
     $options = array('id_customer' => $this->obj_customer->id, 'id_service_customer' => $this->obj_customer->id_service_customer, 'period_start' => $sql_period_obj->data[0]["date_start"], 'period_end' => $sql_period_obj->data[0]["date_end"]);
     $csv = new cdr_csv($options);
     if (!($this->output = $csv->getCSV())) {
         log_write("error", "page_output", "Unable to generate CSV ouput for the configured range");
         return 0;
     }
     return 1;
 }
예제 #14
0
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "config_integration";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "admin/config_integration-process.php";
     $this->obj_form->method = "post";
     // customer portal stuff
     $structure = NULL;
     $structure["fieldname"] = "MODULE_CUSTOMER_PORTAL";
     $structure["type"] = "checkbox";
     $structure["options"]["no_translate_fieldname"] = "yes";
     $structure["options"]["label"] = "Enable/disable the customer portal integration.";
     $this->obj_form->add_input($structure);
     // submit section
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Save Changes";
     $this->obj_form->add_input($structure);
     // define subforms
     $this->obj_form->subforms["config_integration"] = array("MODULE_CUSTOMER_PORTAL");
     $this->obj_form->subforms["submit"] = array("submit");
     if (error_check()) {
         // load error datas
         $this->obj_form->load_data_error();
     } else {
         // fetch all the values from the database
         $sql_config_obj = new sql_query();
         $sql_config_obj->string = "SELECT name, value FROM config ORDER BY name";
         $sql_config_obj->execute();
         $sql_config_obj->fetch_array();
         foreach ($sql_config_obj->data as $data_config) {
             $this->obj_form->structure[$data_config["name"]]["defaultvalue"] = $data_config["value"];
         }
         unset($sql_config_obj);
     }
 }
예제 #15
0
 function execute()
 {
     if ($this->mode == 1) {
         /*
         	MODE 1: INITAL FILE UPLOAD
         */
         $this->obj_form = new form_input();
         $this->obj_form->formname = "domain_import";
         $this->obj_form->language = $_SESSION["user"]["lang"];
         $this->obj_form->action = "domains/import-process.php";
         $this->obj_form->method = "post";
         // import type
         $structure = NULL;
         $structure["fieldname"] = "import_upload_type";
         $structure["type"] = "radio";
         $structure["values"] = array("file_bind_8");
         $structure["defaultvalue"] = "file_bind_8";
         $this->obj_form->add_input($structure);
         // file upload
         $structure = NULL;
         $structure["fieldname"] = "import_upload_file";
         $structure["type"] = "file";
         $this->obj_form->add_input($structure);
         // submit section
         $structure = NULL;
         $structure["fieldname"] = "submit";
         $structure["type"] = "submit";
         $structure["defaultvalue"] = "Save Changes";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "mode";
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = $this->mode;
         $this->obj_form->add_input($structure);
         // define subforms
         $this->obj_form->subforms["upload"] = array("import_upload_type", "import_upload_file");
         $this->obj_form->subforms["hidden"] = array("mode");
         $this->obj_form->subforms["submit"] = array("submit");
         // import data
         if (error_check()) {
             $this->obj_form->load_data_error();
         }
     } elseif ($this->mode == 2) {
         /*
         	MODE 2: DOMAIN RECORD ASSIGNMENT
         
         	Information from the imported zone file under mode 1 has been converted and loaded into
         	the session variables, from here we can now enter all that information into a form and
         	the user can correct/complete before we push through to the database.
         
         	We also need to address issues like over-writing of existing domains here.
         */
         /*
         	Define form structure
         */
         $this->obj_form = new form_input();
         $this->obj_form->formname = "domain_import";
         $this->obj_form->language = $_SESSION["user"]["lang"];
         $this->obj_form->action = "domains/import-process.php";
         $this->obj_form->method = "post";
         /*
         	General domain & SOA information
         */
         $structure = NULL;
         $structure["fieldname"] = "domain_type";
         $structure["type"] = "radio";
         $structure["values"] = array("domain_standard", "domain_reverse_ipv4", "domain_reverse_ipv6");
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "domain_standard";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "domain_name";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "ipv4_help";
         $structure["type"] = "text";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "help_ipv4_help";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "ipv6_help";
         $structure["type"] = "text";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "help_ipv6_help";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "ipv4_network";
         $structure["type"] = "input";
         $structure["options"]["help"] = "eg: 192.168.0.0";
         $structure["options"]["label"] = " /24";
         $structure["options"]["req"] = "yes";
         $this->obj_form->add_input($structure);
         /*
         		$structure = NULL;
         		$structure["fieldname"] 	= "ipv4_subnet";
         		$structure["type"]		= "radio";
         		$structure["values"]		= array("24", "16", "8");
         		$structure["options"]["req"]	= "yes";
         		$this->obj_form->add_input($structure);
         
         		$structure = NULL;
         		$structure["fieldname"] 	= "ipv4_autofill";
         		$structure["type"]		= "checkbox";
         		$structure["options"]["label"]	= lang_trans("help_ipv4_autofill");
         		$structure["options"]["req"]	= "yes";
         		$this->obj_form->add_input($structure);
         
         		$structure = NULL;
         		$structure["fieldname"] 	= "ipv4_autofill_domain";
         		$structure["type"]		= "input";
         		$structure["options"]["help"]	= "eg: static.example.com";
         		$structure["options"]["req"]	= "yes";
         		$this->obj_form->add_input($structure);
         */
         $structure = NULL;
         $structure["fieldname"] = "ipv6_network";
         $structure["type"] = "input";
         $structure["options"]["help"] = "eg: 2001:db8::/48";
         $structure["options"]["label"] = " always include a /cidr value (/1 though to /64)";
         $structure["options"]["req"] = "yes";
         $this->obj_form->add_input($structure);
         /*
         			$structure = NULL;
         			$structure["fieldname"] 	= "ipv6_autofill";
         			$structure["type"]		= "checkbox";
         			$structure["options"]["label"]	= lang_trans("help_ipv6_autofill");
         			$structure["options"]["req"]	= "yes";
         			$this->obj_form->add_input($structure);
         
         			$structure = NULL;
         			$structure["fieldname"] 	= "ipv6_autofill_forward";
         			$structure["type"]		= "checkbox";
         			$structure["options"]["label"]	= lang_trans("help_ipv6_autofill_forward");
         			$structure["options"]["req"]	= "yes";
         			$this->obj_form->add_input($structure);
         
         			$structure = NULL;
         			$structure["fieldname"] 	= "ipv6_autofill_reverse_from_forward";
         			$structure["type"]		= "checkbox";
         			$structure["options"]["label"]	= lang_trans("help_ipv6_autofill_reverse_from_forward");
         			$structure["options"]["req"]	= "yes";
         			$this->obj_form->add_input($structure);
         
         			$structure = NULL;
         			$structure["fieldname"] 	= "ipv6_autofill_domain";
         			$structure["type"]		= "input";
         			$structure["options"]["help"]	= "eg: static.example.com";
         			$structure["options"]["req"]	= "yes";
         			$this->obj_form->add_input($structure);
         */
         $this->obj_form->add_action("domain_type", "default", "domain_name", "show");
         $this->obj_form->add_action("domain_type", "default", "ipv4_help", "hide");
         $this->obj_form->add_action("domain_type", "default", "ipv4_network", "hide");
         //		$this->obj_form->add_action("domain_type", "default", "ipv4_subnet", "hide");
         //		$this->obj_form->add_action("domain_type", "default", "ipv4_autofill", "hide");
         $this->obj_form->add_action("domain_type", "default", "ipv6_help", "hide");
         $this->obj_form->add_action("domain_type", "default", "ipv6_network", "hide");
         //		$this->obj_form->add_action("domain_type", "default", "ipv4_subnet", "hide");
         //		$this->obj_form->add_action("domain_type", "default", "ipv4_autofill", "hide");
         $this->obj_form->add_action("domain_type", "domain_standard", "domain_name", "show");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "domain_name", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv4_help", "show");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv4_network", "show");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv4_subnet", "show");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv4_autofill", "show");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv6_help", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv6_network", "hide");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv6_subnet", "hide");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv4", "ipv6_autofill", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "domain_name", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv4_help", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv4_network", "hide");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv4_subnet", "hide");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv4_autofill", "hide");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv6_help", "show");
         $this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv6_network", "show");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv6_subnet", "hide");
         //		$this->obj_form->add_action("domain_type", "domain_reverse_ipv6", "ipv6_autofill", "hide");
         //		$this->obj_form->add_action("ipv4_autofill", "default", "ipv4_autofill_domain", "hide");
         //		$this->obj_form->add_action("ipv4_autofill", "1", "ipv4_autofill_domain", "show");
         $structure = NULL;
         $structure["fieldname"] = "domain_description";
         $structure["type"] = "textarea";
         $this->obj_form->add_input($structure);
         // SOA configuration
         $structure = NULL;
         $structure["fieldname"] = "soa_hostmaster";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = $GLOBALS["config"]["DEFAULT_HOSTMASTER"];
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "soa_serial";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = date("Ymd") . "01";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "soa_refresh";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "21600";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "soa_retry";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "3600";
         $this->obj_form->add_input($structure);
         $structure["fieldname"] = "soa_expire";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = "604800";
         $this->obj_form->add_input($structure);
         $structure["fieldname"] = "soa_default_ttl";
         $structure["type"] = "input";
         $structure["options"]["req"] = "yes";
         $structure["defaultvalue"] = $GLOBALS["config"]["DEFAULT_TTL_SOA"];
         $this->obj_form->add_input($structure);
         // define main domain subforms
         $this->obj_form->subforms["domain_details"] = array("domain_type", "domain_name", "ipv4_help", "ipv4_network", "ipv6_help", "ipv6_network", "domain_description");
         $this->obj_form->subforms["domain_soa"] = array("soa_hostmaster", "soa_serial", "soa_refresh", "soa_retry", "soa_expire", "soa_default_ttl");
         /*
         	Imported Records
         
         	The record import logic is not as advanced as the regular record handling
         	page, it's primarily intended to display the import and allow correction
         	before submission.
         
         	For more advanced configuration and addition of rows, the user should
         	import the domain and then adjust like normal.
         */
         // subform header
         $this->obj_form->subforms["domain_records"] = array("record_import_guide");
         $structure = NULL;
         $structure["fieldname"] = "record_import_guide";
         $structure["type"] = "message";
         $structure["defaultvalue"] = "<p>" . lang_trans("record_import_guide") . "</p>";
         $this->obj_form->add_input($structure);
         if (empty($_SESSION["error"]["num_records"])) {
             // no records returned
             $structure = NULL;
             $structure["fieldname"] = "record_import_notice";
             $structure["type"] = "message";
             $structure["defaultvalue"] = "<p>" . lang_trans("records_not_imported") . "</p>";
             $structure["options"]["css_row_class"] = "table_highlight_important";
             $this->obj_form->add_input($structure);
             $this->obj_form->subforms["domain_records"][] = "record_import_notice";
         } else {
             // headers
             $this->obj_form->subforms["domain_records"][] = "record_header";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_type";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_ttl";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_prio";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_name";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_content";
             $this->obj_form->subforms_grouped["domain_records"]["record_header"][] = "record_header_import";
             $structure = NULL;
             $structure["fieldname"] = "record_header_type";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_type");
             $this->obj_form->add_input($structure);
             $structure = NULL;
             $structure["fieldname"] = "record_header_ttl";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_ttl");
             $this->obj_form->add_input($structure);
             $structure = NULL;
             $structure["fieldname"] = "record_header_prio";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_prio");
             $this->obj_form->add_input($structure);
             $structure = NULL;
             $structure["fieldname"] = "record_header_name";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_name");
             $this->obj_form->add_input($structure);
             $structure = NULL;
             $structure["fieldname"] = "record_header_content";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_content");
             $this->obj_form->add_input($structure);
             $structure = NULL;
             $structure["fieldname"] = "record_header_import";
             $structure["type"] = "text";
             $structure["defaultvalue"] = lang_trans("record_header_import");
             $this->obj_form->add_input($structure);
             // draw pre-defined nameserver records
             $obj_sql = new sql_query();
             $obj_sql->string = "SELECT server_name FROM name_servers";
             $obj_sql->execute();
             if ($obj_sql->num_rows()) {
                 $obj_sql->fetch_array();
                 $i = 0;
                 foreach ($obj_sql->data as $data_ns) {
                     $i++;
                     // record form items
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_type";
                     $structure["type"] = "text";
                     $structure["defaultvalue"] = "NS";
                     $this->obj_form->add_input($structure);
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_ttl";
                     $structure["type"] = "text";
                     $structure["defaultvalue"] = $GLOBALS["config"]["DEFAULT_TTL_NS"];
                     $this->obj_form->add_input($structure);
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_prio";
                     $structure["type"] = "text";
                     $structure["defaultvalue"] = "";
                     $this->obj_form->add_input($structure);
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_name";
                     $structure["type"] = "text";
                     $structure["defaultvalue"] = "@";
                     $this->obj_form->add_input($structure);
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_content";
                     $structure["type"] = "text";
                     $structure["defaultvalue"] = $data_ns["server_name"];
                     $this->obj_form->add_input($structure);
                     $structure = NULL;
                     $structure["fieldname"] = "ns_" . $i . "_import";
                     $structure["type"] = "checkbox";
                     $structure["defaultvalue"] = "on";
                     $structure["options"]["disabled"] = "yes";
                     $structure["options"]["label"] = "Import";
                     $this->obj_form->add_input($structure);
                     // domain records
                     $this->obj_form->subforms["domain_records"][] = "ns_" . $i;
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_type";
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_ttl";
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_prio";
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_name";
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_content";
                     $this->obj_form->subforms_grouped["domain_records"]["ns_" . $i][] = "ns_" . $i . "_import";
                 }
             }
             // end of pre-defined nameserver loop
             // loop through imported records and create form structure
             for ($i = 0; $i < $_SESSION["error"]["num_records"]; $i++) {
                 $record = $_SESSION["error"]["records"][$i];
                 // record form items
                 $structure = form_helper_prepare_dropdownfromdb("record_" . $i . "_type", "SELECT type as label, type as id FROM `dns_record_types` WHERE type!='SOA'");
                 $structure["options"]["width"] = "100";
                 $structure["defaultvalue"] = $record["type"];
                 $this->obj_form->add_input($structure);
                 if (!$record["ttl"]) {
                     $record["ttl"] = $GLOBALS["config"]["DEFAULT_TTL_OTHER"];
                 }
                 $structure = NULL;
                 $structure["fieldname"] = "record_" . $i . "_ttl";
                 $structure["type"] = "input";
                 $structure["options"]["width"] = "100";
                 $structure["defaultvalue"] = $record["ttl"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "record_" . $i . "_prio";
                 $structure["type"] = "input";
                 $structure["options"]["width"] = "100";
                 $structure["defaultvalue"] = $record["prio"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "record_" . $i . "_name";
                 $structure["type"] = "input";
                 $structure["defaultvalue"] = $record["name"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "record_" . $i . "_content";
                 $structure["type"] = "input";
                 $structure["defaultvalue"] = $record["content"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "record_" . $i . "_import";
                 $structure["type"] = "checkbox";
                 $structure["defaultvalue"] = "on";
                 $structure["options"]["label"] = "Import";
                 $this->obj_form->add_input($structure);
                 // domain records
                 $this->obj_form->subforms["domain_records"][] = "record_" . $i;
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_type";
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_ttl";
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_prio";
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_name";
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_content";
                 $this->obj_form->subforms_grouped["domain_records"]["record_" . $i][] = "record_" . $i . "_import";
             }
         }
         /*
         	Unmatched Lines Report
         
         	Sadly it's not always possible to import *every* line of ever zone file out there - the styles can vary
         	by far too much to match at times.
         
         	We have a section of the form to display the records which do not match so that users are notified and thus
         	able to make corrections if needed.
         */
         // subform header
         $this->obj_form->subforms["unmatched_import"] = array("unmatched_import_help", "unmatched_import_notice");
         $structure = NULL;
         $structure["fieldname"] = "unmatched_import_help";
         $structure["type"] = "message";
         $structure["defaultvalue"] = "<p>" . lang_trans("unmatched_import_help") . "</p>";
         $this->obj_form->add_input($structure);
         if (empty($_SESSION["error"]["unmatched"])) {
             // no unmatched rows
             $structure = NULL;
             $structure["fieldname"] = "unmatched_import_notice";
             $structure["type"] = "message";
             $structure["defaultvalue"] = "<p>" . lang_trans("import_notice_no_unmatched_rows") . "</p>";
             $structure["options"]["css_row_class"] = "table_highlight_open";
             $this->obj_form->add_input($structure);
         } else {
             // import notice
             $structure = NULL;
             $structure["fieldname"] = "unmatched_import_notice";
             $structure["type"] = "message";
             $structure["defaultvalue"] = "<p>" . lang_trans("import_notice_unmatched_rows") . "</p>";
             $structure["options"]["css_row_class"] = "table_highlight_important";
             $this->obj_form->add_input($structure);
             // add all the unmatched rows
             for ($i = 0; $i < count($_SESSION["error"]["unmatched"]); $i++) {
                 $this->obj_form->subforms["unmatched_import"][] = "unmatched_row_{$i}";
                 $structure = NULL;
                 $structure["fieldname"] = "unmatched_row_{$i}";
                 $structure["type"] = "message";
                 $structure["defaultvalue"] = "\"" . $_SESSION["error"]["unmatched"][$i] . "\"";
                 $this->obj_form->add_input($structure);
             }
         }
         // end of unmatched lines loop
         /*
         	Submission
         */
         // submit section
         $structure = NULL;
         $structure["fieldname"] = "submit";
         $structure["type"] = "submit";
         $structure["defaultvalue"] = "Save Changes";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "mode";
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = $this->mode;
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "num_records";
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = $_SESSION["error"]["num_records"];
         $this->obj_form->add_input($structure);
         // define submit subforms
         $this->obj_form->subforms["hidden"] = array("mode", "num_records");
         $this->obj_form->subforms["submit"] = array("submit");
         // import data
         //			if (error_check())
         //			{
         //				$_SESSION["error"]["form"]["domain_import"] = "error";
         //				$this->obj_form->load_data_error();
         //			}
         foreach (array_keys($this->obj_form->structure) as $fieldname) {
             if (isset($_SESSION["error"][$fieldname])) {
                 $this->obj_form->structure[$fieldname]["defaultvalue"] = stripslashes($_SESSION["error"][$fieldname]);
             }
         }
     }
     // end of mode
 }
예제 #16
0
 function execute()
 {
     log_debug("invoice_form_delete", "Executing execute()");
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT locked, amount_paid FROM account_" . $this->type . " WHERE id='" . $this->invoiceid . "' LIMIT 1";
     $sql_obj->execute();
     $sql_obj->fetch_array();
     $this->locked = $sql_obj->data[0]["locked"];
     $this->amount_paid = $sql_obj->data[0]["amount_paid"];
     /*
     	Start Form
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = $this->type . "_invoice_delete";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = $this->processpage;
     $this->obj_form->method = "POST";
     /*
     	Define form structure
     */
     // basic details
     $structure = NULL;
     $structure["fieldname"] = "code_invoice";
     $structure["type"] = "text";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "delete_confirm";
     $structure["type"] = "checkbox";
     $structure["options"]["label"] = "Yes, I wish to delete this invoice and realise that once deleted the data can not be recovered.";
     $this->obj_form->add_input($structure);
     // hidden fields
     $structure = NULL;
     $structure["fieldname"] = "date_create";
     $structure["type"] = "hidden";
     $this->obj_form->add_input($structure);
     // ID
     $structure = NULL;
     $structure["fieldname"] = "id_invoice";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->invoiceid;
     $this->obj_form->add_input($structure);
     // submit
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Delete Invoice";
     $this->obj_form->add_input($structure);
     // load data
     $this->obj_form->sql_query = "SELECT date_create, code_invoice, locked, amount_paid FROM account_" . $this->type . " WHERE id='" . $this->invoiceid . "'";
     $this->obj_form->load_data();
     $this->obj_form->subforms[$this->type . "_invoice_delete"] = array("code_invoice");
     $this->obj_form->subforms["hidden"] = array("id_invoice", "date_create");
     if ($this->locked) {
         $this->obj_form->subforms["submit"] = array("");
     } else {
         $this->obj_form->subforms["submit"] = array("delete_confirm", "submit");
     }
 }
예제 #17
0
 function render_html()
 {
     // calcuate next/previous week/year
     if ($this->date_selected_weekofyear == 1) {
         $date_option_previousyear = $this->date_selected_year - 1;
         $date_option_previousweek = 52;
         $date_option_nextyear = $this->date_selected_year;
         $date_option_nextweek = 2;
     } elseif ($this->date_selected_weekofyear == 52) {
         $date_option_previousyear = $this->date_selected_year;
         $date_option_previousweek = 51;
         $date_option_nextyear = $this->date_selected_year + 1;
         $date_option_nextweek = 1;
     } else {
         $date_option_previousyear = $this->date_selected_year;
         $date_option_previousweek = $this->date_selected_weekofyear - 1;
         $date_option_nextyear = $this->date_selected_year;
         $date_option_nextweek = $this->date_selected_weekofyear + 1;
     }
     // Week view header
     print "<h3>TIME REGISTRATION</h3><br><br>";
     /*
     	Unbilled Time
     */
     if (user_permissions_get("projects_timegroup")) {
         /*
         	Create an array of all unbilled time records. We need to do the following to create this list:
         	1. Exclude any internal_only projects.
         	2. Include time which belongs to a time_group, but ONLY if the time group has not been added to an invoice.
         */
         $unbilled_ids = array();
         // select non-internal projects
         $sql_projects_obj = new sql_query();
         $sql_projects_obj->string = "SELECT projects.id as projectid, project_phases.id as phaseid FROM project_phases LEFT JOIN projects ON projects.id = project_phases.projectid WHERE projects.internal_only='0'";
         $sql_projects_obj->execute();
         if ($sql_projects_obj->num_rows()) {
             $sql_projects_obj->fetch_array();
             foreach ($sql_projects_obj->data as $project_data) {
                 // select non-group time records
                 $sql_obj = new sql_query();
                 $sql_obj->string = "SELECT id FROM timereg WHERE groupid='0' AND phaseid='" . $project_data["phaseid"] . "'";
                 $sql_obj->execute();
                 if ($sql_obj->num_rows()) {
                     $sql_obj->fetch_array();
                     foreach ($sql_obj->data as $data_tmp) {
                         // we store the ID inside an array key, since they are unique
                         // and this will prevent us needed to check for the existance of
                         // the ID already.
                         $unbilled_ids[$data_tmp["id"]] = "on";
                     }
                 }
                 unset($sql_obj);
                 // select unpaid group IDs
                 $sql_obj = new sql_query();
                 $sql_obj->string = "SELECT id FROM time_groups WHERE projectid='" . $project_data["projectid"] . "' AND invoiceid='0'";
                 $sql_obj->execute();
                 if ($sql_obj->num_rows()) {
                     $sql_obj->fetch_array();
                     foreach ($sql_obj->data as $data_group) {
                         // fetch all the time reg IDs belonging this group, but only select time entries marked as billable - we
                         // don't want to report a timegroup with unbillable time as being billed!
                         $sql_reg_obj = new sql_query();
                         $sql_reg_obj->string = "SELECT id FROM timereg WHERE groupid='" . $data_group["id"] . "' AND billable='1'";
                         $sql_reg_obj->execute();
                         if ($sql_reg_obj->num_rows()) {
                             $sql_reg_obj->fetch_array();
                             foreach ($sql_reg_obj->data as $data_tmp) {
                                 // we store the ID inside an array key, since they are unique
                                 // and this will prevent us needed to check for the existance of
                                 // the ID already.
                                 $unbilled_ids[$data_tmp["id"]] = "on";
                             }
                         }
                         unset($sql_reg_obj);
                     }
                 }
                 unset($sql_obj);
             }
         }
         // fetch amount of unbilled time
         $sql_obj = new sql_query();
         $sql_obj->prepare_sql_settable("timereg");
         $sql_obj->prepare_sql_addfield("timebooked", "SUM(timereg.time_booked)");
         if ($this->access_staff_ids) {
             $sql_obj->prepare_sql_addwhere("employeeid IN (" . format_arraytocommastring($this->access_staff_ids) . ")");
         }
         $sql_obj->prepare_sql_addjoin("LEFT JOIN time_groups ON timereg.groupid = time_groups.id");
         // provide list of valid IDs
         $unbilled_ids_keys = array_keys($unbilled_ids);
         $unbilled_ids_count = count($unbilled_ids_keys);
         $unbilled_ids_sql = "";
         if ($unbilled_ids_count) {
             $i = 0;
             foreach ($unbilled_ids_keys as $id) {
                 $i++;
                 if ($i == $unbilled_ids_count) {
                     $unbilled_ids_sql .= "timereg.id='{$id}' ";
                 } else {
                     $unbilled_ids_sql .= "timereg.id='{$id}' OR ";
                 }
             }
             $sql_obj->prepare_sql_addwhere("({$unbilled_ids_sql})");
             $sql_obj->generate_sql();
             $sql_obj->execute();
             $sql_obj->fetch_array();
             list($unbilled_time_hours, $unbilled_time_mins) = explode(":", time_format_hourmins($sql_obj->data[0]["timebooked"]));
             if ($unbilled_time_hours > 0 && $unbilled_time_mins > 0) {
                 $message = "There are currently {$unbilled_time_hours} hours and {$unbilled_time_mins} minutes of unbilled time to be processed. Click here to view.";
             } elseif ($unbilled_time_hours > 0) {
                 $message = "There are currently {$unbilled_time_hours} hours of unbilled time to be processed. Click here to view.";
             } elseif ($unbilled_time_mins > 0) {
                 $message = "There are currently {$unbilled_time_mins} minutes of unbilled time to be processed. Click here to view.";
             }
         } else {
             $message = "There is no unbilled time to be processed.";
         }
         // display
         print "<br>";
         format_linkbox("default", "index.php?page=timekeeping/unbilled.php", "<p><b>UNBILLED TIME</b></p><p>{$message}</p>");
     }
     /*end unbilled time*/
     print "<br />";
     /*
     Time booked
     */
     // fetch amount of time booked for today
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("timereg");
     $sql_obj->prepare_sql_addfield("timebooked", "SUM(timereg.time_booked)");
     $sql_obj->prepare_sql_addwhere("date='" . date("Y-m-d") . "'");
     if ($this->access_staff_ids) {
         $sql_obj->prepare_sql_addwhere("employeeid IN (" . format_arraytocommastring($this->access_staff_ids) . ")");
     }
     $sql_obj->generate_sql();
     $sql_obj->execute();
     $sql_obj->fetch_array();
     list($booked_time_hours, $booked_time_mins) = explode(":", time_format_hourmins($sql_obj->data[0]["timebooked"]));
     if ($booked_time_hours > 0 && $booked_time_mins > 0) {
         $message = "<b>Time booked for today: {$booked_time_hours} hours and {$booked_time_mins} minutes.</b><br />Click here to add more time.";
     } elseif ($booked_time_hours > 0) {
         $message = "<b>Time booked for today: {$booked_time_hours} hours.</b><br />Click here to add more time.";
     } elseif ($booked_time_mins > 0) {
         $message = "<b>Time booked for today: {$booked_time_mins} minutes.</b><br />Click here to add more time.";
     } else {
         $message = "<b>No time has been booked for today</b><br />Click here to add time.</b>";
     }
     format_linkbox("default", "index.php?page=timekeeping/timereg-day-edit.php", "<p>{$message}</p>");
     print "<br />";
     print "<table class=\"table_highlight\" width=\"100%\"><tr>";
     // Week selection links
     print "<td width=\"70%\">";
     print "<b>WEEK " . $this->date_selected_weekofyear . ", " . $this->date_selected_year . "</b><br>";
     print "(" . time_format_humandate($this->date_selected_start) . " to " . time_format_humandate($this->date_selected_end) . ")<br>";
     print "<br>";
     print "<p><b>";
     print "<a class=\"button\" href=\"index.php?page=timekeeping/timereg.php&employeeid=" . $this->employeeid . "&weekofyear=" . $date_option_previousweek . "&year=" . $date_option_previousyear . "\">&lt;&lt; Previous Week</a>";
     // check for date in the future
     if ($this->config_timesheet_booktofuture == "disabled") {
         if (time_date_to_timestamp(time_calculate_weekstart($date_option_nextweek, $date_option_nextyear)) < time()) {
             // end date is in not in the future
             print " <a class=\"button\" href=\"index.php?page=timekeeping/timereg.php&employeeid=" . $this->employeeid . "&weekofyear=" . $date_option_nextweek . "&year=" . $date_option_nextyear . "\">Next Week &gt;&gt;</a>";
         }
     } else {
         print " <a class=\"button\" href=\"index.php?page=timekeeping/timereg.php&employeeid=" . $this->employeeid . "&weekofyear=" . $date_option_nextweek . "&year=" . $date_option_nextyear . "\">Next Week &gt;&gt;</a>";
     }
     print "</b></p>";
     print "</td>";
     // goto date form
     print "<td width=\"30%\">";
     print "<form method=\"get\" action=\"index.php\" class=\"form_standard\">";
     $this->obj_form_goto->render_field("date");
     print "<br>";
     $this->obj_form_goto->render_field("page");
     $this->obj_form_goto->render_field("submit");
     print "</form>";
     print "</td>";
     print "</tr></table><br>";
     // Employee selection form
     //
     // we use a custom form display method here, since the normal form
     // class will draw a fully styled form in a table.
     //
     if ($this->employeeid) {
         print "<table class=\"table_highlight\" width=\"100%\"><tr><td width=\"100%\">";
     } else {
         print "<table class=\"table_highlight_important\" width=\"100%\"><tr><td width=\"100%\">";
     }
     print "<form method=\"get\" action=\"index.php\" class=\"form_standard\">";
     print "<p><b>Select an employee to view:</b></p>";
     $this->obj_form_employee->render_field("employeeid");
     $this->obj_form_employee->render_field("weekofyear");
     $this->obj_form_employee->render_field("year");
     $this->obj_form_employee->render_field("page");
     $this->obj_form_employee->render_field("submit");
     print "</form>";
     print "</td></tr></table><br>";
     if ($this->employeeid) {
         // custom labels and links
         if ($this->config_timesheet_booktofuture == "disabled") {
             if (time_date_to_timestamp($this->date_selected_daysofweek[0]) < time()) {
                 $this->obj_table_week->custom_column_link("monday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[0] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[1]) < time()) {
                 $this->obj_table_week->custom_column_link("tuesday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[1] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[2]) < time()) {
                 $this->obj_table_week->custom_column_link("wednesday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[2] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[3]) < time()) {
                 $this->obj_table_week->custom_column_link("thursday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[3] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[4]) < time()) {
                 $this->obj_table_week->custom_column_link("friday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[4] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[5]) < time()) {
                 $this->obj_table_week->custom_column_link("saturday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[5] . "");
             }
             if (time_date_to_timestamp($this->date_selected_daysofweek[6]) < time()) {
                 $this->obj_table_week->custom_column_link("sunday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[6] . "");
             }
         } else {
             // add links
             $this->obj_table_week->custom_column_link("monday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[0] . "");
             $this->obj_table_week->custom_column_link("tuesday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[1] . "");
             $this->obj_table_week->custom_column_link("wednesday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[2] . "");
             $this->obj_table_week->custom_column_link("thursday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[3] . "");
             $this->obj_table_week->custom_column_link("friday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[4] . "");
             $this->obj_table_week->custom_column_link("saturday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[5] . "");
             $this->obj_table_week->custom_column_link("sunday", "index.php?page=timekeeping/timereg-day.php&date=" . $this->date_selected_daysofweek[6] . "");
         }
         // column labels
         $this->obj_table_week->custom_column_label("monday", "Monday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[0]) . ")</font>");
         $this->obj_table_week->custom_column_label("tuesday", "Tuesday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[1]) . ")</font>");
         $this->obj_table_week->custom_column_label("wednesday", "Wednesday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[2]) . ")</font>");
         $this->obj_table_week->custom_column_label("thursday", "Thursday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[3]) . ")</font>");
         $this->obj_table_week->custom_column_label("friday", "Friday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[4]) . ")</font>");
         $this->obj_table_week->custom_column_label("saturday", "Saturday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[5]) . ")</font>");
         $this->obj_table_week->custom_column_label("sunday", "Sunday<br><font style=\"font-size: 8px;\">(" . time_format_humandate($this->date_selected_daysofweek[6]) . ")</font>");
         // display week time table
         $this->obj_table_week->render_table_html();
         print "<table width=\"100%\">";
         // add time link
         if (user_permissions_staff_get("timereg_write", $this->employeeid)) {
             print "<td align=\"left\" valign=\"top\"><p><a class=\"button\" href=\"index.php?page=timekeeping/timereg-day-edit.php\">Add new time entry</a></p></td>";
         } else {
             print "<p><i>You have read-only access to this employee and therefore can not add any more time.</i></p>";
         }
         // display CSV/PDF download link
         print "<td align=\"right\">";
         print "<p><a class=\"button_export\" href=\"index-export.php?mode=csv&page=timekeeping/timereg.php\">Export as CSV</a></p>";
         print "<p><a class=\"button_export\" href=\"index-export.php?mode=pdf&page=timekeeping/timereg.php\">Export as PDF</a></p>";
         print "</td>";
         print "</table>";
     }
 }
예제 #18
0
 function execute()
 {
     /*
     	Create an array of all unbilled time records. We need to do the following to create this list:
     	1. Exclude any internal_only projects.
     	2. Include time which belongs to a time_group, but ONLY if the time group has not been added to an invoice.
     */
     $unbilled_ids = array();
     // select non-internal projects
     $sql_projects_obj = new sql_query();
     $sql_projects_obj->string = "SELECT projects.id as projectid, project_phases.id as phaseid FROM project_phases LEFT JOIN projects ON projects.id = project_phases.projectid WHERE projects.internal_only='0'";
     $sql_projects_obj->execute();
     if ($sql_projects_obj->num_rows()) {
         $sql_projects_obj->fetch_array();
         foreach ($sql_projects_obj->data as $project_data) {
             // select non-group time records
             $sql_obj = new sql_query();
             $sql_obj->string = "SELECT id FROM timereg WHERE groupid='0' AND phaseid='" . $project_data["phaseid"] . "'";
             $sql_obj->execute();
             if ($sql_obj->num_rows()) {
                 $sql_obj->fetch_array();
                 foreach ($sql_obj->data as $data_tmp) {
                     // we store the ID inside an array key, since they are unique
                     // and this will prevent us needed to check for the existance of
                     // the ID already.
                     $unbilled_ids[$data_tmp["id"]] = "on";
                 }
             }
             unset($sql_obj);
             // select unpaid group IDs
             $sql_obj = new sql_query();
             $sql_obj->string = "SELECT id FROM time_groups WHERE projectid='" . $project_data["projectid"] . "' AND invoiceid='0'";
             $sql_obj->execute();
             if ($sql_obj->num_rows()) {
                 $sql_obj->fetch_array();
                 foreach ($sql_obj->data as $data_group) {
                     // fetch all the time reg IDs belonging this group, but only select time entries marked as billable - we
                     // don't want to report a timegroup with unbillable time as being billed!
                     $sql_reg_obj = new sql_query();
                     $sql_reg_obj->string = "SELECT id FROM timereg WHERE groupid='" . $data_group["id"] . "' AND billable='1'";
                     $sql_reg_obj->execute();
                     if ($sql_reg_obj->num_rows()) {
                         $sql_reg_obj->fetch_array();
                         foreach ($sql_reg_obj->data as $data_tmp) {
                             // we store the ID inside an array key, since they are unique
                             // and this will prevent us needed to check for the existance of
                             // the ID already.
                             $unbilled_ids[$data_tmp["id"]] = "on";
                         }
                     }
                     unset($sql_reg_obj);
                 }
             }
             unset($sql_obj);
         }
     }
     /*
     	Define table
     */
     // establish a new table object
     $this->obj_table = new table();
     $this->obj_table->language = $_SESSION["user"]["lang"];
     $this->obj_table->tablename = "timereg_unbilled";
     // define all the columns and structure
     $this->obj_table->add_column("date", "date", "timereg.date");
     $this->obj_table->add_column("standard", "name_phase", "CONCAT_WS(' -- ', projects.code_project, projects.name_project, project_phases.name_phase)");
     $this->obj_table->add_column("standard", "name_staff", "CONCAT_WS(' -- ', staff.staff_code, staff.name_staff)");
     $this->obj_table->add_column("standard", "time_group", "time_groups.name_group");
     $this->obj_table->add_column("standard", "description", "timereg.description");
     $this->obj_table->add_column("hourmins", "time_booked", "timereg.time_booked");
     // defaults
     $this->obj_table->columns = array("date", "name_phase", "name_staff", "time_group", "description", "time_booked");
     $this->obj_table->columns_order = array("date", "name_phase");
     $this->obj_table->columns_order_options = array("date", "name_phase", "name_staff", "time_group", "description");
     // define SQL structure
     $this->obj_table->sql_obj->prepare_sql_settable("timereg");
     $this->obj_table->sql_obj->prepare_sql_addfield("id", "timereg.id");
     $this->obj_table->sql_obj->prepare_sql_addfield("projectid", "projects.id");
     $this->obj_table->sql_obj->prepare_sql_addfield("employeeid", "timereg.employeeid");
     $this->obj_table->sql_obj->prepare_sql_addfield("timegroupid", "time_groups.id");
     $this->obj_table->sql_obj->prepare_sql_addfield("timegroupinvoiceid", "time_groups.invoiceid");
     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN staff ON timereg.employeeid = staff.id");
     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN time_groups ON timereg.groupid = time_groups.id");
     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN project_phases ON timereg.phaseid = project_phases.id");
     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN projects ON project_phases.projectid = projects.id");
     // provide list of valid IDs
     $unbilled_ids_keys = array_keys($unbilled_ids);
     $unbilled_ids_count = count($unbilled_ids_keys);
     $unbilled_ids_sql = "";
     if ($unbilled_ids_count) {
         $this->obj_table->sql_obj->prepare_sql_addwhere("timereg.id IN (" . format_arraytocommastring($unbilled_ids_keys) . ")");
     }
     // if the user only has access to specific staff, filter to these staff members
     if ($this->access_staff_ids) {
         $this->obj_table->sql_obj->prepare_sql_addwhere("timereg.employeeid IN (" . format_arraytocommastring($this->access_staff_ids) . ")");
     }
     /// Filtering/Display Options
     // fixed options
     $this->obj_table->add_fixed_option("id", $this->id);
     // acceptable filter options
     $structure = NULL;
     $structure["fieldname"] = "date_start";
     $structure["type"] = "date";
     $structure["sql"] = "date >= 'value'";
     $this->obj_table->add_filter($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_end";
     $structure["type"] = "date";
     $structure["sql"] = "date <= 'value'";
     $this->obj_table->add_filter($structure);
     $structure = form_helper_prepare_dropdownfromdb("phaseid", "SELECT \n\t\t\t\t\t\t\t\t\t\t\tprojects.code_project as label,\n\t\t\t\t\t\t\t\t\t\t\tprojects.name_project as label1,\n\t\t\t\t\t\t\t\t\t\t\tproject_phases.id as id, \n\t\t\t\t\t\t\t\t\t\t\tproject_phases.name_phase as label1\n\t\t\t\t\t\t\t\t\t\tFROM `projects` \n\t\t\t\t\t\t\t\t\t\tLEFT JOIN project_phases ON project_phases.projectid = projects.id\n\t\t\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t\t\tprojects.internal_only='0'\n\t\t\t\t\t\t\t\t\t\tORDER BY\n\t\t\t\t\t\t\t\t\t\t\tprojects.name_project,\n\t\t\t\t\t\t\t\t\t\t\tproject_phases.name_phase");
     $structure["sql"] = "project_phases.id='value'";
     $structure["options"]["search_filter"] = "yes";
     $this->obj_table->add_filter($structure);
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("staff");
     $sql_obj->prepare_sql_addfield("id", "id");
     $sql_obj->prepare_sql_addfield("label", "staff_code");
     $sql_obj->prepare_sql_addfield("label1", "name_staff");
     if ($this->access_staff_ids) {
         $sql_obj->prepare_sql_addwhere("id IN (" . format_arraytocommastring($this->access_staff_ids) . ")");
     }
     $sql_obj->generate_sql();
     $structure = form_helper_prepare_dropdownfromdb("employeeid", $sql_obj->string);
     $structure["sql"] = "timereg.employeeid='value'";
     $structure["options"]["search_filter"] = "yes";
     $this->obj_table->add_filter($structure);
     $structure = NULL;
     $structure["fieldname"] = "searchbox";
     $structure["type"] = "input";
     $structure["sql"] = "(timereg.description LIKE '%value%' OR project_phases.name_phase LIKE '%value%' OR staff.name_staff LIKE '%value%')";
     $this->obj_table->add_filter($structure);
     $structure = NULL;
     $structure["fieldname"] = "groupby";
     $structure["type"] = "radio";
     $structure["values"] = array("none", "name_phase", "name_staff");
     $structure["defaultvalue"] = "none";
     $this->obj_table->add_filter($structure);
     // create totals
     $this->obj_table->total_columns = array("time_booked");
     // load options form
     $this->obj_table->load_options_form();
     // add group by options
     if ($this->obj_table->filter["filter_groupby"]["defaultvalue"] != "none") {
         $this->obj_table->sql_obj->prepare_sql_addgroupby($this->obj_table->filter["filter_groupby"]["defaultvalue"]);
         // replace timereg value with SUM query
         $this->obj_table->structure["time_booked"]["dbname"] = "SUM(timereg.time_booked)";
         switch ($this->obj_table->filter["filter_groupby"]["defaultvalue"]) {
             case "name_staff":
                 $this->obj_table->columns = array("name_staff", "time_booked");
                 $this->obj_table->columns_order = array();
                 $this->obj_table->columns_order_options = array("name_staff");
                 break;
             case "name_phase":
                 $this->obj_table->columns = array("name_phase", "time_booked");
                 $this->obj_table->columns_order = array();
                 $this->obj_table->columns_order_options = array("name_phase");
                 break;
         }
     }
     // generate & execute SQL query	(only if time entries exist)
     $this->obj_table->generate_sql();
     if ($unbilled_ids_count) {
         $this->obj_table->load_data_sql();
     }
     // delete any rows which belong to processed time groups
     for ($i = 0; $i < $this->obj_table->data_num_rows; $i++) {
         if ($this->obj_table->data[$i]["timegroupinvoiceid"]) {
             $this->obj_table->data[$i] = NULL;
         }
     }
 }
예제 #19
0
 $sql_obj = new sql_query();
 $sql_obj->string = "SELECT id FROM `projects` WHERE id='{$projectid}' LIMIT 1";
 $sql_obj->execute();
 if (!$sql_obj->num_rows()) {
     log_write("error", "process", "The project you have attempted to edit - {$projectid} - does not exist in this system.");
 } else {
     if ($phaseid) {
         $mode = "edit";
         // are we editing an existing phase? make sure it exists and belongs to this project
         $sql_obj = new sql_query();
         $sql_obj->string = "SELECT projectid FROM `project_phases` WHERE id='{$phaseid}' LIMIT 1";
         $sql_obj->execute();
         if (!$sql_obj->num_rows()) {
             log_write("error", "process", "The phase you have attempted to edit - {$phaseid} - does not exist in this system.");
         } else {
             $sql_obj->fetch_array();
             if ($sql_obj->data[0]["projectid"] != $projectid) {
                 log_write("error", "process", "The requested phase does not match the provided project ID. Potential application bug?");
             }
         }
     } else {
         $mode = "add";
     }
 }
 //// ERROR CHECKING ///////////////////////
 // make sure we don't choose a phase name that has already been used for this project
 // Note: we don't mind if this phase name is the same as other phases in DIFFERENT projects
 $sql_obj = new sql_query();
 $sql_obj->string = "SELECT id FROM `project_phases` WHERE name_phase='" . $data["name_phase"] . "' AND projectid='{$projectid}' ";
 if ($phaseid) {
     $sql_obj->string .= " AND id!='{$phaseid}'";
예제 #20
0
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "transaction_view";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "accounts/gl/edit-process.php";
     $this->obj_form->method = "post";
     // general
     $structure = NULL;
     $structure["fieldname"] = "code_gl";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_trans";
     $structure["type"] = "date";
     $structure["defaultvalue"] = date("Y-m-d");
     $structure["options"]["req"] = "yes";
     $this->obj_form->add_input($structure);
     $sql_struct_obj = new sql_query();
     $sql_struct_obj->prepare_sql_settable("staff");
     $sql_struct_obj->prepare_sql_addfield("id", "staff.id");
     $sql_struct_obj->prepare_sql_addfield("label", "staff.staff_code");
     $sql_struct_obj->prepare_sql_addfield("label1", "staff.name_staff");
     $sql_struct_obj->prepare_sql_addorderby("staff_code");
     $sql_struct_obj->prepare_sql_addwhere("id = 'CURRENTID' OR date_end = '0000-00-00'");
     $structure = form_helper_prepare_dropdownfromobj("employeeid", $sql_struct_obj);
     $structure["options"]["req"] = "yes";
     $structure["options"]["autoselect"] = "yes";
     $structure["options"]["width"] = "600";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "description";
     $structure["type"] = "input";
     $structure["options"]["width"] = "600";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "description_useall";
     $structure["type"] = "checkbox";
     $structure["options"]["label"] = "Check this to use the description above as the description in all the rows below. Untick if you wish to have different messages for each transaction item.";
     $structure["defaultvalue"] = "on";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "notes";
     $structure["type"] = "textarea";
     $structure["options"]["width"] = "600";
     $structure["options"]["height"] = "50";
     $this->obj_form->add_input($structure);
     /*
     	Define transaction form structure
     */
     // unless there has been error data returned, fetch all the transactions
     // from the DB, and work out the number of rows
     if (!isset($_SESSION["error"]["form"][$this->obj_form->formname])) {
         $sql_trans_obj = new sql_query();
         $sql_trans_obj->string = "SELECT date_trans, amount_debit, amount_credit, chartid, source, memo FROM `account_trans` WHERE type='gl' AND customid='" . $this->id . "'";
         $sql_trans_obj->execute();
         if ($sql_trans_obj->num_rows()) {
             $sql_trans_obj->fetch_array();
             $this->num_trans = $sql_trans_obj->data_num_rows + 1;
         }
     } else {
         $this->num_trans = @security_script_input('/^[0-9]*$/', $_SESSION["error"]["num_trans"]) + 1;
     }
     // ensure there are always 2 rows at least, additional rows are added if required (ie viewing
     // an existing transaction) or on the fly when needed by javascript UI.
     if ($this->num_trans < 2) {
         $this->num_trans = 2;
     }
     // transaction rows
     for ($i = 0; $i < $this->num_trans; $i++) {
         // account
         $structure = form_helper_prepare_dropdownfromdb("trans_" . $i . "_account", "SELECT id, code_chart as label, description as label1 FROM account_charts WHERE chart_type!='1' ORDER BY code_chart");
         $structure["options"]["width"] = "200";
         $this->obj_form->add_input($structure);
         // debit field
         $structure = NULL;
         $structure["fieldname"] = "trans_" . $i . "_debit";
         $structure["type"] = "input";
         $structure["options"]["width"] = "80";
         $this->obj_form->add_input($structure);
         // credit field
         $structure = NULL;
         $structure["fieldname"] = "trans_" . $i . "_credit";
         $structure["type"] = "input";
         $structure["options"]["width"] = "80";
         $this->obj_form->add_input($structure);
         // source
         $structure = NULL;
         $structure["fieldname"] = "trans_" . $i . "_source";
         $structure["type"] = "input";
         $structure["options"]["width"] = "100";
         $this->obj_form->add_input($structure);
         // description
         $structure = NULL;
         $structure["fieldname"] = "trans_" . $i . "_description";
         $structure["type"] = "textarea";
         $this->obj_form->add_input($structure);
         // if we have data from a sql query, load it in
         if ($sql_trans_obj->data_num_rows) {
             if (isset($sql_trans_obj->data[$i]["chartid"])) {
                 $this->obj_form->structure["trans_" . $i . "_debit"]["defaultvalue"] = $sql_trans_obj->data[$i]["amount_debit"];
                 $this->obj_form->structure["trans_" . $i . "_credit"]["defaultvalue"] = $sql_trans_obj->data[$i]["amount_credit"];
                 $this->obj_form->structure["trans_" . $i . "_account"]["defaultvalue"] = $sql_trans_obj->data[$i]["chartid"];
                 $this->obj_form->structure["trans_" . $i . "_source"]["defaultvalue"] = $sql_trans_obj->data[$i]["source"];
                 $this->obj_form->structure["trans_" . $i . "_description"]["defaultvalue"] = $sql_trans_obj->data[$i]["memo"];
             }
         }
     }
     // total fields
     $structure = NULL;
     $structure["fieldname"] = "total_debit";
     $structure["type"] = "hidden";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "total_credit";
     $structure["type"] = "hidden";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "money_format";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = format_money(0);
     $this->obj_form->add_input($structure);
     // hidden
     $structure = NULL;
     $structure["fieldname"] = "id_transaction";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "num_trans";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = "{$this->num_trans}";
     $this->obj_form->add_input($structure);
     // submit section
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Save Changes";
     $this->obj_form->add_input($structure);
     // fetch the general form data
     $this->obj_form->sql_query = "SELECT * FROM `account_gl` WHERE id='" . $this->id . "' LIMIT 1";
     $this->obj_form->load_data();
     // calculate totals
     for ($i = 0; $i < $this->num_trans; $i++) {
         @($this->obj_form->structure["total_debit"]["defaultvalue"] += $this->obj_form->structure["trans_" . $i . "_debit"]["defaultvalue"]);
         @($this->obj_form->structure["total_credit"]["defaultvalue"] += $this->obj_form->structure["trans_" . $i . "_credit"]["defaultvalue"]);
     }
 }
예제 #21
0
 function execute()
 {
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "customer_view";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "customers/edit-process.php";
     $this->obj_form->method = "post";
     // general
     $structure = NULL;
     $structure["fieldname"] = "code_customer";
     $structure["type"] = "input";
     $structure["options"]["req"] = "yes";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "name_customer";
     $structure["type"] = "input";
     $structure["options"]["req"] = "yes";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_start";
     $structure["type"] = "date";
     $structure["options"]["req"] = "yes";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_end";
     $structure["type"] = "date";
     $this->obj_form->add_input($structure);
     //contacts
     // this should be replaced with load_data_contacts in some point in the future.
     $sql_contacts_obj = new sql_query();
     $sql_contacts_obj->string = "SELECT id, role, contact, description FROM customer_contacts WHERE customer_id = " . $this->id;
     $sql_contacts_obj->execute();
     $sql_contacts_obj->fetch_array();
     if (!empty($_SESSION["error"]["num_contacts"])) {
         $this->num_contacts = stripslashes($_SESSION["error"]["num_contacts"]);
     } else {
         if ($sql_contacts_obj->num_rows()) {
             $this->num_contacts = $sql_contacts_obj->num_rows();
         } else {
             $this->num_contacts = 0;
         }
     }
     $structure = NULL;
     $structure["fieldname"] = "num_contacts";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->num_contacts;
     $this->obj_form->add_input($structure);
     for ($i = 0; $i < $this->num_contacts; $i++) {
         $structure = NULL;
         $structure["fieldname"] = "contact_id_" . $i;
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = $sql_contacts_obj->data[$i]["id"];
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "delete_contact_" . $i;
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = "false";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "contact_" . $i;
         $structure["type"] = "input";
         $structure["defaultvalue"] = $sql_contacts_obj->data[$i]["contact"];
         if (isset($_SESSION["error"]["contact_" . $i . "-error"])) {
             $structure["options"]["css_field_class"] = "hidden_form_field_error";
         } else {
             $structure["options"]["css_field_class"] = "hidden_form_field";
         }
         $structure["options"]["width"] = "200";
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "role_" . $i;
         $structure["type"] = "dropdown";
         $structure["defaultvalue"] = $sql_contacts_obj->data[$i]["role"];
         $structure["options"]["width"] = "205";
         if ($i == 0) {
             $structure["values"] = array("accounts");
             $structure["options"]["autoselect"] = "yes";
             $structure["options"]["disabled"] = "yes";
         } else {
             $structure["values"] = array("other");
             $structure["options"]["autoselect"] = "yes";
         }
         if (isset($_SESSION["error"]["contact_" . $i . "-error"])) {
             $structure["options"]["css_field_class"] = "hidden_form_field_error";
         } else {
             $structure["options"]["css_field_class"] = "hidden_form_field";
         }
         $this->obj_form->add_input($structure);
         $structure = NULL;
         $structure["fieldname"] = "description_" . $i;
         $structure["type"] = "textarea";
         $structure["defaultvalue"] = $sql_contacts_obj->data[$i]["description"];
         if (isset($_SESSION["error"]["contact_" . $i . "-error"])) {
             $structure["options"]["css_field_class"] = "hidden_form_field_error";
         } else {
             $structure["options"]["css_field_class"] = "hidden_form_field";
         }
         $structure["options"]["width"] = "205";
         $structure["options"]["height"] = "";
         $this->obj_form->add_input($structure);
         //contact records
         $sql_records_obj = new sql_query();
         if (!empty($sql_contacts_obj->data[$i]["id"])) {
             $sql_records_obj->string = "SELECT id, type, label, detail FROM customer_contact_records WHERE contact_id= " . $sql_contacts_obj->data[$i]["id"] . " ORDER BY type";
             $sql_records_obj->execute();
             $sql_records_obj->fetch_array();
         }
         if (!empty($_SESSION["error"]["num_records_{$i}"])) {
             $num_records = stripslashes($_SESSION["error"]["num_records_{$i}"]);
         } else {
             if ($sql_records_obj->num_rows()) {
                 $num_records = $sql_records_obj->num_rows();
             } else {
                 $num_records = 0;
             }
         }
         $structure = NULL;
         $structure["fieldname"] = "num_records_" . $i;
         $structure["type"] = "hidden";
         $structure["defaultvalue"] = $num_records;
         $this->obj_form->add_input($structure);
         if ($num_records > 0) {
             for ($j = 0; $j < $num_records; $j++) {
                 $structure = NULL;
                 $structure["fieldname"] = "contact_" . $i . "_record_id_" . $j;
                 $structure["type"] = "hidden";
                 $structure["defaultvalue"] = $sql_records_obj->data[$j]["id"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "contact_" . $i . "_delete_" . $j;
                 $structure["type"] = "hidden";
                 $structure["defaultvalue"] = "false";
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "contact_" . $i . "_type_" . $j;
                 $structure["type"] = "hidden";
                 $structure["defaultvalue"] = $sql_records_obj->data[$j]["type"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "contact_" . $i . "_label_" . $j;
                 $structure["type"] = "hidden";
                 $structure["defaultvalue"] = $sql_records_obj->data[$j]["label"];
                 $this->obj_form->add_input($structure);
                 $structure = NULL;
                 $structure["fieldname"] = "contact_" . $i . "_detail_" . $j;
                 $structure["type"] = "input";
                 $structure["defaultvalue"] = $sql_records_obj->data[$j]["detail"];
                 $structure["options"]["width"] = "";
                 $this->obj_form->add_input($structure);
             }
         }
     }
     // taxes
     $structure = NULL;
     $structure["fieldname"] = "tax_number";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure = form_helper_prepare_dropdownfromdb("tax_default", "SELECT id, name_tax as label FROM account_taxes");
     $this->obj_form->add_input($structure);
     // list all the taxes so the user can enable or disable the taxes
     $sql_tax_obj = new sql_query();
     $sql_tax_obj->string = "SELECT id, name_tax, description FROM account_taxes ORDER BY name_tax";
     $sql_tax_obj->execute();
     if ($sql_tax_obj->num_rows()) {
         // user note
         $structure = NULL;
         $structure["fieldname"] = "tax_message";
         $structure["type"] = "message";
         $structure["defaultvalue"] = "<p>Select all the taxes below which apply to this customer. Any taxes not selected, will not be added to invoices for this customer.</p>";
         $this->obj_form->add_input($structure);
         // fetch customer's current tax status
         if (!isset($_SESSION["error"]["message"])) {
             $sql_customer_taxes_obj = new sql_query();
             $sql_customer_taxes_obj->string = "SELECT taxid FROM customers_taxes WHERE customerid='" . $this->id . "'";
             $sql_customer_taxes_obj->execute();
             if ($sql_customer_taxes_obj->num_rows()) {
                 $sql_customer_taxes_obj->fetch_array();
             }
         }
         // run through all the taxes
         $sql_tax_obj->fetch_array();
         foreach ($sql_tax_obj->data as $data_tax) {
             // define tax checkbox
             $structure = NULL;
             $structure["fieldname"] = "tax_" . $data_tax["id"];
             $structure["type"] = "checkbox";
             $structure["options"]["label"] = $data_tax["name_tax"] . " -- " . $data_tax["description"];
             $structure["options"]["no_fieldname"] = "enable";
             // check if this tax is currently checked
             if ($sql_customer_taxes_obj->data_num_rows) {
                 foreach ($sql_customer_taxes_obj->data as $data) {
                     if ($data["taxid"] == $data_tax["id"]) {
                         $structure["defaultvalue"] = "on";
                     }
                 }
             }
             // add to form
             $this->obj_form->add_input($structure);
             $this->tax_array[] = "tax_" . $data_tax["id"];
         }
     } else {
         $structure = NULL;
         $structure["fieldname"] = "tax_message";
         $structure["type"] = "message";
         $structure["defaultvalue"] = "<p>No taxes can be selected for this customer, as there have been no taxes configured yet.</p>";
         $this->obj_form->add_input($structure);
     }
     // purchase options
     $structure = NULL;
     $structure["fieldname"] = "discount";
     $structure["type"] = "input";
     $structure["options"]["width"] = 50;
     $structure["options"]["label"] = " %";
     $structure["options"]["max_length"] = "6";
     $this->obj_form->add_input($structure);
     // billing address
     $structure = NULL;
     $structure["fieldname"] = "address1_street";
     $structure["type"] = "textarea";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address1_city";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address1_state";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address1_country";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address1_zipcode";
     $structure["type"] = "input";
     $this->obj_form->add_input($structure);
     // shipping address
     $structure = NULL;
     $structure["fieldname"] = "address1_same_as_2";
     $structure["type"] = "checkbox";
     $structure["options"]["label"] = " " . lang_trans("address1_same_as_2_help");
     $structure["options"]["css_row_class"] = "shipping_same_address";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address2_street";
     $structure["type"] = "textarea";
     $structure["options"]["css_row_class"] = "shipping_address";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address2_city";
     $structure["type"] = "input";
     $structure["options"]["css_row_class"] = "shipping_address";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address2_state";
     $structure["type"] = "input";
     $structure["options"]["css_row_class"] = "shipping_address";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address2_country";
     $structure["type"] = "input";
     $structure["options"]["css_row_class"] = "shipping_address";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "address2_zipcode";
     $structure["type"] = "input";
     $structure["options"]["css_row_class"] = "shipping_address";
     $this->obj_form->add_input($structure);
     // billing options
     $structure = NULL;
     $structure["fieldname"] = "billing_method";
     $structure["type"] = "radio";
     $structure["values"] = array("manual", "direct debit");
     //, "credit card");
     $structure["defaultvalue"] = "standalone";
     $structure["options"]["css_row_class"] = "billing_method";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "billing_direct_debit";
     $structure["type"] = "input";
     $structure["options"]["css_row_class"] = "billing_direct_debit";
     $this->obj_form->add_input($structure);
     // reseller options
     $structure = NULL;
     $structure["fieldname"] = "reseller_customer";
     $structure["type"] = "radio";
     $structure["values"] = array("standalone", "reseller", "customer_of_reseller");
     $structure["defaultvalue"] = "standalone";
     $structure["options"]["css_row_class"] = "reseller_customer";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure = form_helper_prepare_dropdownfromdb("reseller_id", "SELECT id, code_customer as label, name_customer as label1 FROM customers WHERE reseller_customer = 'reseller' AND id <> " . $this->id);
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "reseller_message";
     $structure["type"] = "message";
     $structure["defaultvalue"] = "<p>Each customer in the system can either be classified for the Reseller Options as either a standalone customer, a reseller to other customers, or as a customer of a reseller. By default new customers are standalone.</p>";
     $this->obj_form->add_input($structure);
     // submit section
     if (user_permissions_get("customers_write")) {
         $structure = NULL;
         $structure["fieldname"] = "submit";
         $structure["type"] = "submit";
         $structure["defaultvalue"] = "Save Changes";
         $this->obj_form->add_input($structure);
     }
     // hidden
     $structure = NULL;
     $structure["fieldname"] = "id_customer";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     // fetch the form data
     $this->obj_form->sql_query = "SELECT * FROM `customers` WHERE id='" . $this->id . "' LIMIT 1";
     $this->obj_form->load_data();
 }
     // handle service information
     $obj_sql = new sql_query();
     $obj_sql->string = "UPDATE services_customers SET date_period_first='" . $data["date_period_first"] . "',  date_period_next='" . $data["date_period_next"] . "' WHERE id='" . $obj_customer->id_service_customer . "' LIMIT 1";
     $obj_sql->execute();
     unset($obj_sql);
     // delete service periods
     $obj_sql = new sql_query();
     $obj_sql->string = "DELETE FROM services_customers_periods WHERE id_service_customer='" . $obj_customer->id_service_customer . "'";
     $obj_sql->execute();
     unset($obj_sql);
     // handle any bundle member services
     $obj_component_sql = new sql_query();
     $obj_component_sql->string = "SELECT id FROM services_customers WHERE bundleid='" . $obj_customer->id_service_customer . "'";
     $obj_component_sql->execute();
     if ($obj_component_sql->num_rows()) {
         $obj_component_sql->fetch_array();
         foreach ($obj_component_sql->data as $data_component) {
             // adjust service dates
             $obj_sql = new sql_query();
             $obj_sql->string = "UPDATE services_customers SET date_period_first='" . $data["date_period_first"] . "',  date_period_next='" . $data["date_period_next"] . "' WHERE id='" . $data_component["id"] . "' LIMIT 1";
             $obj_sql->execute();
             // delete service periods
             $obj_sql = new sql_query();
             $obj_sql->string = "DELETE FROM services_customers_periods WHERE id_service_customer='" . $id_component["id"] . "'";
             $obj_sql->execute();
         }
     }
     unset($obj_component_sql);
 }
 // handle end date
 if (!empty($data["date_period_last"])) {
예제 #23
0
 function action_update()
 {
     log_debug("inc_taxes", "Executing action_update()");
     /*
     	Start SQL Transaction
     */
     $sql_obj = new sql_query();
     $sql_obj->trans_begin();
     /*
     	If no ID exists, create a new tax first
     */
     if (!$this->id) {
         $mode = "create";
         if (!$this->action_create()) {
             $sql_obj->trans_rollback();
             log_write("error", "inc_taxes", "An error occured whilst attempting to create the new tax. No changes were made.");
             return 0;
         }
     } else {
         $mode = "update";
     }
     /*
     	Update tax details
     */
     $sql_obj->string = "UPDATE `account_taxes` SET " . "name_tax='" . $this->data["name_tax"] . "', " . "taxrate='" . $this->data["taxrate"] . "', " . "chartid='" . $this->data["chartid"] . "', " . "taxnumber='" . $this->data["taxnumber"] . "', " . "description='" . $this->data["description"] . "', " . "default_customers='" . $this->data["default_customers"] . "', " . "default_vendors='" . $this->data["default_vendors"] . "', " . "default_products='" . $this->data["default_products"] . "', " . "default_services='" . $this->data["default_services"] . "' " . "WHERE id='{$this->id}'";
     $sql_obj->execute();
     /*
     	Create customer/vendor/product/service tax selection mappings if requested
     */
     if (!empty($this->data["autoenable_tax_customers"])) {
         // loop through customers
         $sql_cust_obj = new sql_query();
         $sql_cust_obj->string = "SELECT id FROM customers";
         $sql_cust_obj->execute();
         if ($sql_cust_obj->num_rows()) {
             $sql_cust_obj->fetch_array();
             foreach ($sql_cust_obj->data as $data_customer) {
                 // insert tax assignment for this customer
                 $sql_obj->string = "INSERT INTO customers_taxes (customerid, taxid) VALUES ('" . $data_customer["id"] . "', '" . $this->id . "')";
                 $sql_obj->execute();
             }
         }
     }
     if (!empty($this->data["autoenable_tax_vendors"])) {
         // loop through vendors
         $sql_vendor_obj = new sql_query();
         $sql_vendor_obj->string = "SELECT id FROM vendors";
         $sql_vendor_obj->execute();
         if ($sql_vendor_obj->num_rows()) {
             $sql_vendor_obj->fetch_array();
             foreach ($sql_vendor_obj->data as $data_vendor) {
                 // insert tax assignment for this vendor
                 $sql_obj->string = "INSERT INTO vendors_taxes (vendorid, taxid) VALUES ('" . $data_vendor["id"] . "', '" . $this->id . "')";
                 $sql_obj->execute();
             }
         }
     }
     if (!empty($this->data["autoenable_tax_products"])) {
         // loop through products
         $sql_product_obj = new sql_query();
         $sql_product_obj->string = "SELECT id FROM products";
         $sql_product_obj->execute();
         if ($sql_product_obj->num_rows()) {
             $sql_product_obj->fetch_array();
             foreach ($sql_product_obj->data as $data_product) {
                 // insert tax assignment for this product
                 $sql_obj->string = "INSERT INTO products_taxes (productid, taxid) VALUES ('" . $data_product["id"] . "', '" . $this->id . "')";
                 $sql_obj->execute();
             }
         }
     }
     if (!empty($this->data["autoenable_tax_services"])) {
         // loop through services
         $sql_service_obj = new sql_query();
         $sql_service_obj->string = "SELECT id FROM services";
         $sql_service_obj->execute();
         if ($sql_service_obj->num_rows()) {
             $sql_service_obj->fetch_array();
             foreach ($sql_service_obj->data as $data_service) {
                 // insert tax assignment for this service
                 $sql_obj->string = "INSERT INTO services_taxes (serviceid, taxid) VALUES ('" . $data_service["id"] . "', '" . $this->id . "')";
                 $sql_obj->execute();
             }
         }
     }
     /*
     	Commit
     */
     if (error_check()) {
         $sql_obj->trans_rollback();
         log_write("error", "inc_taxes", "An error occured whilst attempting to update the tax. No changes have been made.");
         return 0;
     } else {
         $sql_obj->trans_commit();
         if ($mode == "update") {
             log_write("notification", "inc_taxes", "Tax successfully updated.");
         } else {
             log_write("notification", "inc_taxes", "Tax successfully created.");
         }
         return $this->id;
     }
 }
 //send reminder
 if ($send_reminder) {
     //fetch invoice id
     $invoice_id = @security_form_input_predefined("int", "invoice_id_{$i}", 1, "A problem occurred - no id seems to exist for this invoice");
     //fetch days overdue
     $days_overdue = @security_form_input_predefined("int", "days_overdue_{$i}", 0, "");
     //fetch basic invoice details
     $obj_sql_invoice = new sql_query();
     $obj_sql_invoice->string = "SELECT code_invoice, customerid FROM account_ar WHERE id='" . $invoice_id . "' LIMIT 1";
     $obj_sql_invoice->execute();
     $obj_sql_invoice->fetch_array();
     //fetch basic customer details
     $obj_sql_contact = new sql_query();
     $obj_sql_contact->string = "SELECT id, contact FROM customer_contacts WHERE customer_id = '" . $obj_sql_invoice->data[0]["customerid"] . "' AND role = 'accounts'";
     $obj_sql_contact->execute();
     $obj_sql_contact->fetch_array();
     //fetch email to address, set error if no address is set
     $to = sql_get_singlevalue("SELECT detail AS value FROM customer_contact_records WHERE contact_id = '" . $obj_sql_contact->data[0]["id"] . "' AND type = 'email' LIMIT 1");
     if (!$to) {
         $error_array[] = $obj_sql_invoice->data[0]["code_invoice"];
         continue;
     }
     //create invoice
     $obj_invoice = new invoice();
     $obj_invoice->type = "ar";
     $obj_invoice->id = $invoice_id;
     $obj_invoice->load_data();
     $obj_invoice->load_data_export();
     //get templating keys and values
     $invoice_data = $obj_invoice->invoice_fields;
     $invoice_data_parts['keys'] = array_keys($invoice_data);
예제 #25
0
 function execute()
 {
     /*
     	Check if the project can be deleted or not
     */
     // see if any time has been booked to any of the phases
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT id FROM project_phases WHERE projectid='" . $this->id . "'";
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         foreach ($sql_obj->data as $phase_data) {
             $sql_phase_obj = new sql_query();
             $sql_phase_obj->string = "SELECT id FROM timereg WHERE phaseid='" . $phase_data["id"] . "' LIMIT 1";
             $sql_phase_obj->execute();
             if ($sql_phase_obj->num_rows()) {
                 $this->locked = 1;
             }
         }
     }
     /*
     	Define form structure
     */
     $this->obj_form = new form_input();
     $this->obj_form->formname = "project_delete";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     $this->obj_form->action = "projects/delete-process.php";
     $this->obj_form->method = "post";
     // general
     $structure = NULL;
     $structure["fieldname"] = "name_project";
     $structure["type"] = "text";
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "code_project";
     $structure["type"] = "text";
     $this->obj_form->add_input($structure);
     // hidden
     $structure = NULL;
     $structure["fieldname"] = "id_project";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $this->id;
     $this->obj_form->add_input($structure);
     // confirm delete
     $structure = NULL;
     $structure["fieldname"] = "delete_confirm";
     $structure["type"] = "checkbox";
     $structure["options"]["label"] = "Yes, I wish to delete this project and realise that once deleted the data can not be recovered.";
     $this->obj_form->add_input($structure);
     // submit button
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "delete";
     $this->obj_form->add_input($structure);
     // define subforms
     $this->obj_form->subforms["project_delete"] = array("code_project", "name_project");
     $this->obj_form->subforms["hidden"] = array("id_project");
     if ($this->locked) {
         $this->obj_form->subforms["submit"] = array();
     } else {
         $this->obj_form->subforms["submit"] = array("delete_confirm", "submit");
     }
     // fetch the form data
     $this->obj_form->sql_query = "SELECT * FROM `projects` WHERE id='" . $this->id . "' LIMIT 1";
     $this->obj_form->load_data();
 }
예제 #26
0
 function execute()
 {
     /*
     	Filter selection form
     */
     // fetch existing values
     $this->date_start = @security_script_input("/^[0-9]*-[0-9]*-[0-9]*\$/", $_GET["date_start_yyyy"] . "-" . $_GET["date_start_mm"] . "-" . $_GET["date_start_dd"]);
     $this->date_end = @security_script_input("/^[0-9]*-[0-9]*-[0-9]*\$/", $_GET["date_end_yyyy"] . "-" . $_GET["date_end_mm"] . "-" . $_GET["date_end_dd"]);
     $this->mode = @security_script_input("/^\\S*\$/", $_GET["mode"]);
     if (!$this->mode) {
         if ($_SESSION["account_reports"]["mode"]) {
             $this->mode = $_SESSION["account_reports"]["mode"];
         } else {
             $this->mode = "Accrual/Invoice";
         }
     }
     if (!$this->date_start || $this->date_start == "--") {
         if ($_SESSION["account_reports"]["date_start"]) {
             $this->date_start = $_SESSION["account_reports"]["date_start"];
         } else {
             $this->date_start = NULL;
         }
     }
     if (!$this->date_end || $this->date_end == "--") {
         if ($_SESSION["account_reports"]["date_end"]) {
             $this->date_end = $_SESSION["account_reports"]["date_end"];
         } else {
             $this->date_end = NULL;
         }
     }
     // save to session vars
     $_SESSION["account_reports"]["date_start"] = $this->date_start;
     $_SESSION["account_reports"]["date_end"] = $this->date_end;
     $_SESSION["account_reports"]["mode"] = $this->mode;
     // define form
     $this->obj_form = new form_input();
     $this->obj_form->method = "get";
     $this->obj_form->action = "index.php";
     $this->obj_form->formname = "accounts_report_incomestatement";
     $this->obj_form->language = $_SESSION["user"]["lang"];
     // hidden values
     $structure = NULL;
     $structure["fieldname"] = "page";
     $structure["type"] = "hidden";
     $structure["defaultvalue"] = $_GET["page"];
     $this->obj_form->add_input($structure);
     // date selection
     $structure = NULL;
     $structure["fieldname"] = "date_start";
     $structure["type"] = "date";
     $structure["defaultvalue"] = $this->date_start;
     $this->obj_form->add_input($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_end";
     $structure["type"] = "date";
     $structure["defaultvalue"] = $this->date_end;
     $this->obj_form->add_input($structure);
     // mode selection
     $structure = NULL;
     $structure["fieldname"] = "mode";
     $structure["type"] = "radio";
     $structure["values"] = array("Accrual/Invoice", "Cash");
     $structure["defaultvalue"] = $this->mode;
     $this->obj_form->add_input($structure);
     // submit
     $structure = NULL;
     $structure["fieldname"] = "submit";
     $structure["type"] = "submit";
     $structure["defaultvalue"] = "Apply Filter Options";
     $this->obj_form->add_input($structure);
     /*
     	Income Charts
     */
     // chart details
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_charts");
     $sql_obj->prepare_sql_addfield("id");
     $sql_obj->prepare_sql_addfield("code_chart");
     $sql_obj->prepare_sql_addfield("description");
     $sql_obj->prepare_sql_addwhere("chart_type='5'");
     $sql_obj->generate_sql();
     $sql_obj->execute();
     $sql_obj->fetch_array();
     $this->data_income = $sql_obj->data;
     unset($sql_obj);
     /*
     	Expense Charts
     */
     // chart details
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_charts");
     $sql_obj->prepare_sql_addfield("id");
     $sql_obj->prepare_sql_addfield("code_chart");
     $sql_obj->prepare_sql_addfield("description");
     $sql_obj->prepare_sql_addwhere("chart_type='6'");
     $sql_obj->generate_sql();
     $sql_obj->execute();
     $sql_obj->fetch_array();
     $this->data_expense = $sql_obj->data;
     unset($sql_obj);
     /*
     	Amounts
     
     	This section needs to total up the the amounts in each account - however, we are unable to just
     	pull the information from account_trans, because we need to be able to fetch either the invoiced/accural
     	amount OR the cash (ie: paid) amount.
     
     	The reports for taxes can handle it simpler, by just calcuating the invoice item total, but we also have
     	to include general ledger transactions.
     
     	Credit notes add additional complexity, however the solution is the same for both accural/invoice and cash
     	basis - read all the items for the credit notes for the selected date range, and subtract from the appropiate
     	account.
     
     	Note that it is entirely possible for the credit notes to end up being more than the invoice amount for a selected
     	time period, in which case, the account may go into negative, which is acceptable.
     
     
     	Accural/Invoice:
     		1. Run though all invoices
     		2. Add item amounts to accounts
     		3. Run through all credit notes during period
     		4. Subtract credit notes falling during period
     		5. Run through all general ledger transactions
     		6. Add GL amounts to accounts
     
     	Cash:
     		1. Run through all invoices
     		2. If invoices are fully paid, then add item amounts to accounts.
     		3. Impossible to handle partially paid invoices properly, so we ignore them.
     		4. Run through all credit notes during period
     		5. Subtract credit notes falling during period
     		6. Run through all general ledger transactions
     		7. Add GL amounts to accounts.
     
     		Note: The date checks are made against the invoice date, not the payment date.
     */
     //
     // AR INVOICES
     //
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_ar");
     $sql_obj->prepare_sql_addfield("id");
     // date options
     if ($this->date_start) {
         $sql_obj->prepare_sql_addwhere("date_trans >= '" . $this->date_start . "'");
     }
     if ($this->date_end) {
         $sql_obj->prepare_sql_addwhere("date_trans <= '" . $this->date_end . "'");
     }
     // paid invoices only
     if ($this->mode == "Cash") {
         $sql_obj->prepare_sql_addwhere("amount_total=amount_paid");
     }
     // run through invoices
     $sql_obj->generate_sql();
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         foreach ($sql_obj->data as $data_invoice) {
             // fetch all items for this invoice type
             $sql_item_obj = new sql_query();
             $sql_item_obj->string = "SELECT chartid, amount FROM account_items WHERE invoiceid='" . $data_invoice["id"] . "' AND invoicetype='ar'";
             $sql_item_obj->execute();
             if ($sql_item_obj->num_rows()) {
                 $sql_item_obj->fetch_array();
                 foreach ($sql_item_obj->data as $data_item) {
                     // run through income charts
                     for ($i = 0; $i < count(array_keys($this->data_income)); $i++) {
                         if ($data_item["chartid"] == $this->data_income[$i]["id"]) {
                             @($this->data_income[$i]["amount"] += $data_item["amount"]);
                         }
                     }
                     // end of loop through charts
                 }
                 // end of invoice item loop
             }
             // end if invoice items
         }
         // end of invoice loop
     }
     // end if invoices
     unset($sql_obj);
     //
     // AR CREDIT NOTES
     //
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_ar_credit");
     $sql_obj->prepare_sql_addfield("id");
     // date options
     if ($this->date_start) {
         $sql_obj->prepare_sql_addwhere("date_trans >= '" . $this->date_start . "'");
     }
     if ($this->date_end) {
         $sql_obj->prepare_sql_addwhere("date_trans <= '" . $this->date_end . "'");
     }
     // run through credit notes
     $sql_obj->generate_sql();
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         foreach ($sql_obj->data as $data_credit) {
             // fetch all items for this invoice type
             $sql_item_obj = new sql_query();
             $sql_item_obj->string = "SELECT chartid, amount FROM account_items WHERE invoiceid='" . $data_credit["id"] . "' AND invoicetype='ar_credit'";
             $sql_item_obj->execute();
             if ($sql_item_obj->num_rows()) {
                 $sql_item_obj->fetch_array();
                 foreach ($sql_item_obj->data as $data_item) {
                     // run through income charts
                     for ($i = 0; $i < count(array_keys($this->data_income)); $i++) {
                         if ($data_item["chartid"] == $this->data_income[$i]["id"]) {
                             @($this->data_income[$i]["amount"] -= $data_item["amount"]);
                         }
                     }
                     // end of loop through charts
                 }
                 // end of credit item loop
             }
             // end of credit items
         }
         // end of credit loop
     }
     // end of credits
     unset($sql_obj);
     //
     // AP INVOICES
     //
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_ap");
     $sql_obj->prepare_sql_addfield("id");
     // date options
     if ($this->date_start) {
         $sql_obj->prepare_sql_addwhere("date_trans >= '" . $this->date_start . "'");
     }
     if ($this->date_end) {
         $sql_obj->prepare_sql_addwhere("date_trans <= '" . $this->date_end . "'");
     }
     // paid invoices only
     if ($this->mode == "Cash") {
         $sql_obj->prepare_sql_addwhere("amount_total=amount_paid");
     }
     // run through invoices
     $sql_obj->generate_sql();
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         foreach ($sql_obj->data as $data_invoice) {
             // fetch all items for this invoice type
             $sql_item_obj = new sql_query();
             $sql_item_obj->string = "SELECT chartid, amount FROM account_items WHERE invoiceid='" . $data_invoice["id"] . "' AND invoicetype='ap'";
             $sql_item_obj->execute();
             if ($sql_item_obj->num_rows()) {
                 $sql_item_obj->fetch_array();
                 foreach ($sql_item_obj->data as $data_item) {
                     // run through expense charts
                     for ($i = 0; $i < count(array_keys($this->data_expense)); $i++) {
                         if ($data_item["chartid"] == $this->data_expense[$i]["id"]) {
                             @($this->data_expense[$i]["amount"] += $data_item["amount"]);
                         }
                     }
                     // end of loop through charts
                 }
                 // end of invoice item loop
             }
             // end if invoice items
         }
         // end of invoice loop
     }
     // end if invoices
     unset($sql_obj);
     //
     // AP CREDIT NOTES
     //
     /*
     	TODO:	AP credit notes have not been implemented at this stage - when they are, an appropiate section will need to be added
     		here to support them.
     */
     //
     // GL TRANSACTIONS
     //
     // Fetch all the GL transactions during this period and add to totals.
     //
     $sql_obj = new sql_query();
     $sql_obj->prepare_sql_settable("account_trans");
     $sql_obj->prepare_sql_addfield("chartid");
     $sql_obj->prepare_sql_addfield("amount_debit");
     $sql_obj->prepare_sql_addfield("amount_credit");
     $sql_obj->prepare_sql_addwhere("type='gl'");
     // date options
     if ($this->date_start) {
         $sql_obj->prepare_sql_addwhere("date_trans >= '" . $this->date_start . "'");
     }
     if ($this->date_end) {
         $sql_obj->prepare_sql_addwhere("date_trans <= '" . $this->date_end . "'");
     }
     // run through GL entries
     $sql_obj->generate_sql();
     $sql_obj->execute();
     if ($sql_obj->num_rows()) {
         $sql_obj->fetch_array();
         foreach ($sql_obj->data as $data_trans) {
             // run through income charts
             for ($i = 0; $i < count(array_keys($this->data_income)); $i++) {
                 if ($data_trans["chartid"] == $this->data_income[$i]["id"]) {
                     $this->data_income[$i]["amount"] += $data_trans["amount_credit"];
                 }
             }
             // end of loop through income charts
             // run through expense charts
             for ($i = 0; $i < count(array_keys($this->data_expense)); $i++) {
                 if ($data_trans["chartid"] == $this->data_expense[$i]["id"]) {
                     $this->data_expense[$i]["amount"] += $data_trans["amount_debit"];
                 }
             }
             // end of loop through expense charts
         }
         // end of transaction loop
     }
     // end if transaction exist
     /*
     	Totals
     */
     // income
     $this->data_totals["income"] = 0;
     for ($i = 0; $i < count(array_keys($this->data_income)); $i++) {
         @($this->data_totals["income"] += $this->data_income[$i]["amount"]);
     }
     // expense
     for ($i = 0; $i < count(array_keys($this->data_expense)); $i++) {
         @($this->data_totals["expense"] += $this->data_expense[$i]["amount"]);
     }
     // final
     $this->data_totals["final"] = @$this->data_totals["income"] - $this->data_totals["expense"];
     $this->data_totals["income"] = @format_money($this->data_totals["income"]);
     $this->data_totals["expense"] = @format_money($this->data_totals["expense"]);
     $this->data_totals["final"] = @format_money($this->data_totals["final"]);
 }
function service_form_delete_process()
{
    log_debug("inc_services_process", "Executing service_form_delete_process()");
    /*
    	Fetch all form data
    */
    // get form data
    $id = @security_form_input_predefined("int", "id_service", 1, "");
    $data["delete_confirm"] = @security_form_input_predefined("any", "delete_confirm", 1, "You must confirm the deletion");
    //// ERROR CHECKING ///////////////////////
    // make sure the service actually exists
    $sql_obj = new sql_query();
    $sql_obj->string = "SELECT id FROM services WHERE id='{$id}' LIMIT 1";
    $sql_obj->execute();
    if (!$sql_obj->num_rows()) {
        log_write("error", "process", "The service you have attempted to edit - {$id} - does not exist in this system.");
    }
    // make sure the service is not active for any customers
    $sql_obj = new sql_query();
    $sql_obj->string = "SELECT id FROM services_customers WHERE serviceid='{$id}' LIMIT 1";
    $sql_obj->execute();
    if ($sql_obj->num_rows()) {
        log_write("error", "process", "Service is active for customers and can therefore not be deleted.");
    }
    /// if there was an error, go back to the entry page
    if ($_SESSION["error"]["message"]) {
        $_SESSION["error"]["form"]["service_delete"] = "failed";
        header("Location: ../index.php?page=services/delete.php&id={$id}");
        exit(0);
    } else {
        /*
        	Begin Transaction
        */
        $sql_obj = new sql_query();
        $sql_obj->trans_begin();
        /*
        	Delete the service data
        */
        $sql_obj->string = "DELETE FROM services WHERE id='{$id}' LIMIT 1";
        $sql_obj->execute();
        /*
        	Delete the service taxes
        */
        $sql_obj->string = "DELETE FROM services_taxes WHERE serviceid='{$id}'";
        $sql_obj->execute();
        /*
        	Delete the service bundle components (if any)
        */
        $sql_bundle_obj = new sql_query();
        $sql_bundle_obj->string = "SELECT id FROM services_bundles WHERE id_service='{$id}'";
        $sql_bundle_obj->execute();
        if ($sql_bundle_obj->num_rows()) {
            $sql_bundle_obj->fetch_array();
            foreach ($sql_bundle_obj->data as $data_bundle) {
                // delete any options for each bundle item
                $sql_obj->string = "DELETE FROM services_options WHERE option_type='service' AND option_type_id='" . $data_bundle["id"] . "'";
                $sql_obj->execute();
            }
        }
        $sql_obj->string = "DELETE FROM services_bundles WHERE id_service='{$id}'";
        $sql_obj->execute();
        /*
        	Delete the service cdr rate overrides (if any)
        */
        $sql_obj->string = "DELETE FROM cdr_rate_tables_overrides WHERE option_type='service' AND option_type_id='{$id}'";
        $sql_obj->execute();
        /*
        	Delete service journal data
        */
        journal_delete_entire("services", $id);
        /*
        	Commit
        */
        if (error_check()) {
            $sql_obj->trans_rollback();
            log_write("error", "process", "An error occured whilst attempting to delete the transaction. No changes have been made.");
            header("Location: ../index.php?page=services/view.php&id={$id}");
            exit(0);
        } else {
            $sql_obj->trans_commit();
            log_write("notification", "process", "Service successfully deleted");
            header("Location: ../index.php?page=services/services.php");
            exit(0);
        }
    }
    // end if passed tests
}
예제 #28
0
 function autopay_credit()
 {
     log_write("debug", "invoice_autopay", "Executing autopay_credit()");
     /*
     	Fetch Current Credit Pool Amount
     
     	NOTE: we do a full query here, to prevent caching from giving us incorrect information.
     */
     $sql_obj = new sql_query();
     $sql_obj->string = "SELECT SUM(amount_total) as value FROM customers_credits WHERE id_customer='" . $this->id_org . "' LIMIT 1";
     $sql_obj->execute();
     $sql_obj->fetch_array();
     $amount_credit = $sql_obj->data[0]["value"];
     unset($sql_obj);
     if ($amount_credit > 0) {
         $this->obj_invoice->data["amount_due"] = $this->obj_invoice->data["amount_total"] - $this->obj_invoice->data["amount_paid"];
         if ($amount_credit > $this->obj_invoice->data["amount_due"]) {
             $amount_credit = $this->obj_invoice->data["amount_due"];
         }
     } else {
         log_write("debug", "invoice_autopay", "No credit available");
         return 0;
     }
     /*
     	Define Item Details
     */
     $item = new invoice_items();
     $item->id_invoice = $this->id_invoice;
     $item->type_invoice = $this->type_invoice;
     $item->type_item = "payment";
     $data = array();
     $data["date_trans"] = date("Y-m-d");
     $data["amount"] = $amount_credit;
     $data["source"] = "CREDIT";
     $data["description"] = "Automated Credit Payment";
     $data["chartid"] = "credit";
     if (!$item->prepare_data($data)) {
         log_write("error", "invoice_autopay", "An error was encountered whilst processing credit payment data.");
         return 0;
     }
     $item->action_create();
     $item->action_update();
     $item->action_update_total();
     $item->action_update_ledger();
     if (!$item->id_item) {
         log_write("error", "invoice_autopay", "An error occured whilst creating the credit autopay item");
     }
     unset($item);
     /*
     	Make Credit Payment Item
     */
     log_write("debug", "invoice_autopay", "Autopayment against invoice " . $this->id_invoice . " made from credit pool.");
     return 1;
 }
예제 #29
0
 function execute()
 {
     // define customer list table
     $this->obj_table_list = new table();
     $this->obj_table_list->language = $_SESSION["user"]["lang"];
     $this->obj_table_list->tablename = "customer_list_billing";
     // define all the columns and structure
     $this->obj_table_list->add_column("standard", "code_customer", "");
     $this->obj_table_list->add_column("standard", "name_customer", "");
     $this->obj_table_list->add_column("standard", "billing_direct_debit", "");
     $this->obj_table_list->add_column("money", "balance_owed", "NONE");
     // defaults
     $this->obj_table_list->columns = array("code_customer", "name_customer", 'billing_direct_debit', 'balance_owed');
     $this->obj_table_list->columns_order = array("name_customer");
     $this->obj_table_list->columns_order_options = array("code_customer", "name_customer", "name_contact", "contact_phone", "contact_mobile", "contact_email", "contact_fax", "date_start", "date_end", "tax_number", "address1_city", "address1_state", "address1_country");
     // define SQL structure
     $this->obj_table_list->sql_obj->prepare_sql_settable("customers");
     $this->obj_table_list->sql_obj->prepare_sql_addfield("id", "");
     /*
                     // define SQL structure
                     $this->obj_table->sql_obj->prepare_sql_settable("account_ar");
                     $this->obj_table->sql_obj->prepare_sql_addfield("id", "account_ar.id");
                     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN customers ON customers.id = account_ar.customerid");
                     $this->obj_table->sql_obj->prepare_sql_addjoin("LEFT JOIN staff ON staff.id = account_ar.employeeid");
     */
     // acceptable filter options
     $structure = NULL;
     $structure["fieldname"] = "date_start";
     $structure["type"] = "date";
     $structure["sql"] = "date_start >= 'value'";
     $this->obj_table_list->add_filter($structure);
     $structure = NULL;
     $structure["fieldname"] = "date_end";
     $structure["type"] = "date";
     $structure["sql"] = "date_end <= 'value' AND date_end != '0000-00-00'";
     $this->obj_table_list->add_filter($structure);
     /*
     $structure = NULL;
     $structure["fieldname"] = "searchbox";
     $structure["type"]	= "input";
     $structure["sql"]	= "(code_customer LIKE '%value%' OR name_customer LIKE '%value%')";
     $this->obj_table_list->add_filter($structure);
     */
     $structure = NULL;
     $structure["fieldname"] = "billing_method";
     $structure["type"] = "checkbox";
     $structure["options"]["label"] = "Billing method Direct Debit";
     $structure["defaultvalue"] = "1";
     $structure["sql"] = "billing_method = 'direct debit'";
     $this->obj_table_list->add_filter($structure);
     // load settings from options form
     $this->obj_table_list->load_options_form();
     // fetch all the customer information
     $this->obj_table_list->generate_sql();
     $this->obj_table_list->load_data_sql();
     // handle balance owed
     if (in_array('balance_owed', $this->obj_table_list->columns)) {
         $obj_balance_owed_sql = new sql_query();
         $obj_balance_owed_sql->string = "SELECT customerid, sum(bal) AS balance_owed FROM (\n\t\t\t\tSELECT ar.customerid, sum(ar.amount_total - ar.amount_paid) as bal \n\t\t\t\tFROM account_ar AS ar \n\t\t\t\tWHERE 1 GROUP BY ar.customerid\n\t\t\t\tUNION\n\t\t\t\tSELECT arc.customerid, sum(arc.amount_total) as bal\n\t\t\t\tFROM account_ar_credit AS arc\n\t\t\t\tWHERE 1 GROUP BY arc.customerid\n\t\t\t\t) as tbl GROUP by customerid";
         $obj_balance_owed_sql->execute();
         if ($obj_balance_owed_sql->num_rows()) {
             $obj_balance_owed_sql->fetch_array();
             foreach ($obj_balance_owed_sql->data as $data_balance_owed) {
                 $map_balance_owed[$data_balance_owed['customerid']] = $data_balance_owed['balance_owed'];
             }
         }
         // replace with 0.00 or the calculated balance value
         for ($i = 0; $i < $this->obj_table_list->data_num_rows; $i++) {
             $this->obj_table_list->data[$i]["balance_owed"] = "0.00";
             if (isset($map_balance_owed[$this->obj_table_list->data[$i]['id']])) {
                 $this->obj_table_list->data[$i]["balance_owed"] = $map_balance_owed[$this->obj_table_list->data[$i]['id']];
             }
             // we dont want 0 balance (or credit) records here
             if ($this->obj_table_list->data[$i]["balance_owed"] <= 0) {
                 unset($this->obj_table_list->data[$i]);
             }
         }
         // re index after the potential unsets
         $this->obj_table_list->data = @array_values($this->obj_table_list->data);
         $this->obj_table_list->data_num_rows = count($this->obj_table_list->data);
         unset($map_balance_owed);
         unset($obj_balance_owed_sql);
     }
 }
function service_invoices_generate($customerid = NULL)
{
    log_debug("inc_services_invoicegen", "Executing service_invoices_generate({$customerid})");
    /*
    	Invoice Report Statistics
    */
    $invoice_stats = array();
    $invoice_stats["time_start"] = time();
    $invoice_stats["total"] = 0;
    $invoice_stats["total_failed"] = 0;
    /*
    	Run through all the customers
    */
    $sql_customers_obj = new sql_query();
    $sql_customers_obj->string = "SELECT id, code_customer, name_customer FROM customers";
    if ($customerid) {
        $sql_customers_obj->string .= " WHERE id='{$customerid}' LIMIT 1";
    }
    $sql_customers_obj->execute();
    if ($sql_customers_obj->num_rows()) {
        $sql_customers_obj->fetch_array();
        foreach ($sql_customers_obj->data as $customer_data) {
            /*
            	Fetch all periods belonging to this customer which need to be billed.
            */
            $sql_periods_obj = new sql_query();
            $sql_periods_obj->string = "SELECT " . "services_customers_periods.id, " . "services_customers_periods.rebill, " . "services_customers_periods.invoiceid, " . "services_customers_periods.invoiceid_usage, " . "services_customers_periods.date_start, " . "services_customers_periods.date_end, " . "services_customers.date_period_first, " . "services_customers.date_period_next, " . "services_customers.date_period_last, " . "services_customers.id as id_service_customer, " . "services_customers.serviceid " . "FROM services_customers_periods " . "LEFT JOIN services_customers ON services_customers.id = services_customers_periods.id_service_customer " . "WHERE " . "services_customers.customerid='" . $customer_data["id"] . "' " . "AND (invoiceid = '0' OR rebill = '1')" . "AND date_billed <= '" . date("Y-m-d") . "'";
            $sql_periods_obj->execute();
            if ($sql_periods_obj->num_rows()) {
                $sql_periods_obj->fetch_array();
                /*
                	BILL CUSTOMER
                
                	This customer has at least one service that needs to be billed. We need to create
                	a new invoice, and then process each service, adding the services to the invoice as 
                	items.
                */
                /*
                	Start Transaction
                
                	(one transaction per invoice)
                */
                $sql_obj = new sql_query();
                $sql_obj->trans_begin();
                /*
                	Create new invoice
                */
                $invoice = new invoice();
                $invoice->type = "ar";
                $invoice->prepare_code_invoice();
                $invoice->data["customerid"] = $customer_data["id"];
                $invoice->data["employeeid"] = 1;
                // set employee to the default internal user
                $invoice->prepare_date_shift();
                if (!$invoice->action_create()) {
                    log_write("error", "services_invoicegen", "Unexpected problem occured whilst attempting to create invoice.");
                    $sql_obj->trans_rollback();
                    return 0;
                }
                $invoiceid = $invoice->id;
                $invoicecode = $invoice->data["code_invoice"];
                unset($invoice);
                /*
                	Create Service Items
                						
                	We need to create an item for basic service plan - IE: the regular fixed fee, and another item for any
                	excess usage.
                */
                foreach ($sql_periods_obj->data as $period_data) {
                    /*
                    	TODO:
                    
                    	We should be able to re-bill usage here when needed with a clever cheat - if we load the periods to be billed,
                    	we can then ignore the plan item, and rebill the usage item.
                    */
                    $period_data["mode"] = "standard";
                    if ($period_data["rebill"]) {
                        if (!$period_data["invoiceid_usage"] && $period_data["invoiceid"]) {
                            // the selected period has been billed, but the usage has been flagged for rebilling - we need to *ignore* the base plan
                            // item and only bill for the usage range.
                            $period_data["mode"] = "rebill_usage";
                        }
                    }
                    // fetch service details
                    $obj_service = new service_bundle();
                    $obj_service->option_type = "customer";
                    $obj_service->option_type_id = $period_data["id_service_customer"];
                    if (!$obj_service->verify_id_options()) {
                        log_write("error", "customers_services", "Unable to verify service ID of " . $period_data["id_service_customer"] . " as being valid.");
                        return 0;
                    }
                    $obj_service->load_data();
                    $obj_service->load_data_options();
                    // ratio is used to adjust prices for partial periods
                    $ratio = 1;
                    if ($obj_service->data["billing_mode_string"] == "monthend" || $obj_service->data["billing_mode_string"] == "monthadvance" || $obj_service->data["billing_mode_string"] == "monthtelco") {
                        log_debug("services_invoicegen", "Invoice bills by month date");
                        /*
                        	Handle monthly billing
                        
                        	Normally, monthly billing is easy, however the very first billing period is special, as it may span a more than 1 month, or
                        	consist of less than the full month, depending on the operational mode.
                        
                        	SERVICE_PARTPERIOD_MODE == seporate
                        
                        		If a service is started on 2008-01-09, the end date of the billing period will be 2008-01-31, which is less
                        		than one month - 22 days.
                        
                        		We can calculate with:
                        
                        		( standard_cost / normal_month_num_days ) * num_days_in_partial_month == total_amount
                        
                        
                        	SERVICE_PARTPERIOD_MODE == merge
                        
                        		If a service is started on 2008-01-09, the end of the billing period will be 2008-02-29, which is 1 month + 21 day.
                        
                        		We can calculate with:
                        
                        		( standard_cost / normal_month_num_days ) * num_days_in_partial_month == extra_amount
                        		total_amount = (extra_amount + normal_amount)
                        
                        	Note: we do not increase included units for licenses service types, since these need to remain fixed and do not
                        	scale with the month.
                        */
                        // check if the period start date is not the first of the month - this means that the period
                        // is an inital partial period.
                        if (time_calculate_daynum($period_data["date_start"]) != "01") {
                            // very first billing month
                            log_write("debug", "services_invoicegen", "First billing month for this service, adjusting pricing to suit days.");
                            // figure out normal period length - rememeber, whilst service may be configured to align monthly, it needs to
                            // handle pricing calculations for variations such as month, year, etc.
                            // get the number of days of a regular period - this correctly handles, month, year, etc
                            // 1. generate the next period dates, this will be of regular length
                            // 2. calculate number of days
                            $tmp_dates = service_period_dates_generate($period_data["date_period_next"], $obj_service->data["billing_cycle_string"], $obj_service->data["billing_mode_string"]);
                            $regular_period_num_days = sql_get_singlevalue("SELECT DATEDIFF('" . $tmp_dates["end"] . "', '" . $tmp_dates["start"] . "') as value");
                            // process for short/long periods
                            //
                            if ($GLOBALS["config"]["SERVICE_PARTPERIOD_MODE"] == "seporate") {
                                log_write("debug", "services_invoicegen", "Adjusting for partial month period (SERVICE_PARTPERIOD_MODE == seporate)");
                                // work out the total number of days in partial month
                                $short_month_days_total = time_calculate_daynum(time_calculate_monthdate_last($period_data["date_start"]));
                                $short_month_days_short = $short_month_days_total - time_calculate_daynum($period_data["date_start"]);
                                log_write("debug", "services_invoicegen", "Short initial billing period of {$short_month_days_short} days");
                                // calculate correct base fee
                                $obj_service->data["price"] = $obj_service->data["price"] / $regular_period_num_days * $short_month_days_short;
                                // calculate ratio
                                $ratio = $short_month_days_short / $regular_period_num_days;
                                log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle short period.");
                            } else {
                                log_write("debug", "services_invoicegen", "Adjusting for extended month period (SERVICE_PARTPERIOD_MODE == merge");
                                // work out the number of days extra
                                $extra_month_days_total = time_calculate_daynum(time_calculate_monthdate_last($period_data["date_start"]));
                                $extra_month_days_extra = $extra_month_days_total - time_calculate_daynum($period_data["date_start"]);
                                log_debug("services_invoicegen", "{$extra_month_days_extra} additional days ontop of started billing period");
                                // calculate correct base fee
                                $obj_service->data["price"] = $obj_service->data["price"] / $regular_period_num_days * $extra_month_days_extra + $obj_service->data["price"];
                                // calculate ratio
                                $ratio = ($extra_month_days_extra + $extra_month_days_total) / $regular_period_num_days;
                                log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle extended period.");
                            }
                        }
                    }
                    /*
                    	Service Last Period Handling
                    
                    	If this is the last period for a service, it may be of a different link to the regular full period term
                    	- this effects both month and period based services.
                    */
                    if ($period_data["date_period_last"] != "0000-00-00") {
                        log_write("debug", "services_invoicegen", "Service has a final period date set (" . $period_data["date_period_last"] . ")");
                        if ($period_data["date_period_last"] == $period_data["date_end"] || time_date_to_timestamp($period_data["date_period_last"]) < time_date_to_timestamp($period_data["date_end"])) {
                            log_write("debug", "services_invoicegen", "Service is a final period, checking for time adjustment (if any)");
                            // fetch the regular end date
                            $orig_dates = service_period_dates_generate($period_data["date_start"], $obj_service->data["billing_cycle_string"], $obj_service->data["billing_mode_string"]);
                            if ($orig_dates["end"] != $period_data["date_end"]) {
                                // work out the total number of days
                                $time = NULL;
                                $time["start"] = time_date_to_timestamp($period_data["date_start"]);
                                $time["end_orig"] = time_date_to_timestamp($orig_dates["end"]);
                                $time["end_new"] = time_date_to_timestamp($period_data["date_end"]);
                                $time["orig_days"] = sprintf("%d", ($time["end_orig"] - $time["start"]) / 86400);
                                $time["new_days"] = sprintf("%d", ($time["end_new"] - $time["start"]) / 86400);
                                log_write("debug", "services_invoicegen", "Short initial billing period of " . $time["new_days"] . " days rather than expected " . $time["orig_days"] . "");
                                // calculate correct base fee
                                $obj_service->data["price"] = $obj_service->data["price"] / $time["orig_days"] * $time["new_days"];
                                // calculate ratio
                                $ratio = $time["new_days"] / $time["orig_days"];
                                log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle short period.");
                                unset($time);
                            } else {
                                log_write("debug", "services_invoicegen", "Final service period is regular size, no adjustment required.");
                            }
                        }
                    }
                    /*
                    	Service Base Plan Item
                    
                    	Note: There can be a suitation where we shouldn't charge for the base plan
                    	fee, when the period end date is older than the service first start date.
                    
                    	This can happen due to the migration mode, which creates a usage-only period
                    	before the actual plan starts properly.
                    */
                    if (time_date_to_timestamp($period_data["date_period_first"]) < time_date_to_timestamp($period_data["date_end"]) && $period_data["mode"] == "standard") {
                        // date of the period is newer than the start date
                        log_write("debug", "inc_service_invoicegen", "Generating base plan item for period " . $period_data["date_start"] . " to " . $period_data["date_end"] . "");
                        // start the item
                        $invoice_item = new invoice_items();
                        $invoice_item->id_invoice = $invoiceid;
                        $invoice_item->type_invoice = "ar";
                        $invoice_item->type_item = "service";
                        $itemdata = array();
                        // chart ID
                        $itemdata["chartid"] = $obj_service->data["chartid"];
                        // service ID
                        $itemdata["customid"] = $obj_service->id;
                        // no units apply
                        $itemdata["units"] = "";
                        // description
                        switch ($obj_service->data["typeid_string"]) {
                            case "phone_single":
                                $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " from " . $period_data["date_start"] . " to " . $period_data["date_end"] . " (" . $obj_service->data["phone_ddi_single"] . ")";
                                if ($obj_service->data["description"]) {
                                    $itemdata["description"] .= "\n\n";
                                    $itemdata["description"] .= addslashes($obj_service->data["description"]);
                                }
                                break;
                            default:
                                $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " from " . $period_data["date_start"] . " to " . $period_data["date_end"];
                                if ($obj_service->data["description"]) {
                                    $itemdata["description"] .= "\n\n";
                                    $itemdata["description"] .= addslashes($obj_service->data["description"]);
                                }
                                break;
                        }
                        // handle final periods
                        if ($period_data["date_period_last"] != "0000-00-00" && $period_data["date_start"] == $period_data["date_end"]) {
                            $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " service terminated as of " . $period_data["date_end"] . "";
                        }
                        // is this service item part of a bundle? if it is, we shouldn't charge for the plan
                        if (!empty($obj_service->data["id_bundle_component"])) {
                            // no charge for the base plan, since this is handled by
                            // the bundle item itself
                            $itemdata["price"] = "0.00";
                            $itemdata["quantity"] = 1;
                            $itemdata["discount"] = "0";
                            // append description
                            $itemdata["description"] .= " (part of bundle)";
                        } else {
                            // amount
                            $itemdata["price"] = $obj_service->data["price"];
                            $itemdata["quantity"] = 1;
                            $itemdata["discount"] = $obj_service->data["discount"];
                        }
                        // create item
                        $invoice_item->prepare_data($itemdata);
                        $invoice_item->action_update();
                        unset($invoice_item);
                    } else {
                        log_write("debug", "inc_service_invoicegen", "Skipping base plan item generation, due to migration or rebill mode operation");
                    }
                    /*
                    	Service Usage Items
                    
                    	Create another item on the invoice for any usage, provided that the service type is a usage service
                    */
                    $period_usage_data = array();
                    /*
                    	Depending on the service type, we need to handle the usage period in one of two ways:
                    	1. Invoice usage for the current period that has just ended (periodend/monthend)
                    	2. Invoice usage for the period BEFORE the current period. (periodtelco/monthtelco)
                    */
                    if ($obj_service->data["billing_mode_string"] == "periodtelco" || $obj_service->data["billing_mode_string"] == "monthtelco") {
                        log_write("debug", "service_invoicegen", "Determining previous period for telco-style usage billing (if any)");
                        if ($period_data["mode"] == "standard") {
                            // fetch previous period (if any) - we can determine the previous by subtracting one day from the current period start date.
                            $tmp_date = explode("-", $period_data["date_start"]);
                            $period_usage_data["date_end"] = date("Y-m-d", mktime(0, 0, 0, $tmp_date[1], $tmp_date[2] - 1, $tmp_date[0]));
                        } elseif ($period_data["mode"] == "rebill_usage") {
                            // use the period as the usage period
                            log_write("debug", "service_invoicegen", "Using the selected period " . $period_usage_data["date_end"] . " for rebill_usage mode");
                            $period_usage_data["date_end"] = $period_data["date_end"];
                        }
                        // fetch period data to confirm previous period
                        $sql_obj = new sql_query();
                        $sql_obj->string = "SELECT id, date_start, date_end FROM services_customers_periods WHERE id_service_customer='" . $period_data["id_service_customer"] . "' AND date_end='" . $period_usage_data["date_end"] . "' LIMIT 1";
                        $sql_obj->execute();
                        if ($sql_obj->num_rows()) {
                            log_write("debug", "service_invoicegen", "Billing for seporate past usage period");
                            // there is a valid usage period
                            $period_usage_data["active"] = "yes";
                            // fetch dates
                            $sql_obj->fetch_array();
                            $period_usage_data["id_service_customer"] = $period_data["id_service_customer"];
                            $period_usage_data["id"] = $sql_obj->data[0]["id"];
                            $period_usage_data["date_start"] = $sql_obj->data[0]["date_start"];
                            $period_usage_data["date_end"] = $sql_obj->data[0]["date_end"];
                            // tracing
                            log_write("debug", "service_invoicegen", "Current period is (" . $period_data["date_start"] . " to " . $period_data["date_end"] . "), usage period is (" . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . ")");
                            // reset ratio
                            $ratio = 1;
                            /*
                            	TODO: This code is a replicant of the section further above for calculating partial
                            	periods and should really be functionalised as part of service usage continual improvements
                            */
                            // calculate usage abnormal period
                            if ($obj_service->data["billing_mode_string"] == "monthend" || $obj_service->data["billing_mode_string"] == "monthadvance" || $obj_service->data["billing_mode_string"] == "monthtelco") {
                                log_debug("services_invoicegen", "Usage period service bills by month date");
                                if (time_calculate_daynum($period_usage_data["date_start"]) != "01") {
                                    // very first billing month
                                    log_write("debug", "services_invoicegen", "First billing month for this usage period, adjusting pricing to suit days.");
                                    if ($GLOBALS["config"]["SERVICE_PARTPERIOD_MODE"] == "seporate") {
                                        log_write("debug", "services_invoicegen", "Adjusting for partial month period (SERVICE_PARTPERIOD_MODE == seporate)");
                                        // work out the total number of days
                                        $short_month_days_total = time_calculate_daynum(time_calculate_monthdate_last($period_usage_data["date_start"]));
                                        $short_month_days_short = $short_month_days_total - time_calculate_daynum($period_usage_data["date_start"]);
                                        log_write("debug", "services_invoicegen", "Short initial billing period of {$short_month_days_short} days");
                                        // calculate ratio
                                        $ratio = $short_month_days_short / $short_month_days_total;
                                        log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle short period.");
                                    } else {
                                        log_write("debug", "services_invoicegen", "Adjusting for extended month period (SERVICE_PARTPERIOD_MODE == merge");
                                        // work out the number of days extra
                                        $extra_month_days_total = time_calculate_daynum(time_calculate_monthdate_last($period_usage_data["date_start"]));
                                        $extra_month_days_extra = $extra_month_days_total - time_calculate_daynum($period_usage_data["date_start"]);
                                        log_debug("services_invoicegen", "{$extra_month_days_extra} additional days ontop of started billing period");
                                        // calculate ratio
                                        $ratio = ($extra_month_days_extra + $extra_month_days_total) / $extra_month_days_total;
                                        log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle extended period.");
                                    }
                                }
                            }
                            // end of calculate usage abnormal period
                            if ($period_data["date_period_last"] != "0000-00-00") {
                                log_write("debug", "services_invoicegen", "Service has a final period date set (" . $period_data["date_period_last"] . ")");
                                if ($period_data["date_period_last"] == $period_usage_data["date_end"] || time_date_to_timestamp($period_data["date_period_last"]) < time_date_to_timestamp($period_usage_data["date_end"])) {
                                    log_write("debug", "services_invoicegen", "Service is a final period, checking for time adjustment (if any)");
                                    // fetch the regular end date
                                    $orig_dates = service_period_dates_generate($period_usage_data["date_start"], $obj_service->data["billing_cycle_string"], $obj_service->data["billing_mode_string"]);
                                    if ($orig_dates["end"] != $period_usage_data["date_end"]) {
                                        // work out the total number of days
                                        $time = NULL;
                                        $time["start"] = time_date_to_timestamp($period_usage_data["date_start"]);
                                        $time["end_orig"] = time_date_to_timestamp($orig_dates["end"]);
                                        $time["end_new"] = time_date_to_timestamp($period_usage_data["date_end"]);
                                        $time["orig_days"] = sprintf("%d", ($time["end_orig"] - $time["start"]) / 86400);
                                        $time["new_days"] = sprintf("%d", ($time["end_new"] - $time["start"]) / 86400);
                                        log_write("debug", "services_invoicegen", "Short initial billing period of " . $time["new_days"] . " days rather than expected " . $time["orig_days"] . "");
                                        // calculate correct base fee
                                        $obj_service->data["price"] = $obj_service->data["price"] / $time["orig_days"] * $time["new_days"];
                                        // calculate ratio
                                        $ratio = $time["new_days"] / $time["orig_days"];
                                        log_write("debug", "services_invoicegen", "Calculated service bill ratio of {$ratio} to handle short period.");
                                        unset($time);
                                    } else {
                                        log_write("debug", "services_invoicegen", "Final service period is regular size, no adjustment required.");
                                    }
                                }
                            }
                        } else {
                            log_write("debug", "service_invoicegen", "Not billing for past usage, as this appears to be the first plan period so no usage can exist yet");
                        }
                    } else {
                        log_write("debug", "service_invoicegen", "Using plan period as data usage period (" . $period_data["date_start"] . " to " . $period_data["date_end"] . "");
                        // use current period
                        $period_usage_data["active"] = "yes";
                        $period_usage_data["id_service_customer"] = $period_data["id_service_customer"];
                        $period_usage_data["id"] = $period_data["id"];
                        $period_usage_data["date_start"] = $period_data["date_start"];
                        $period_usage_data["date_end"] = $period_data["date_end"];
                    }
                    /*
                    	Create usage items if there is a valid usage period
                    */
                    if (!empty($period_usage_data["active"])) {
                        log_write("debug", "service_invoicegen", "Creating usage items due to active usage period");
                        switch ($obj_service->data["typeid_string"]) {
                            case "generic_with_usage":
                                /*
                                	GENERIC_WITH_USAGE
                                
                                	This service is to be used for any non-traffic, non-time accounting service that needs to track usage. Examples of this
                                	could be counting the number of API requests, size of disk usage on a vhost, etc.
                                */
                                log_write("debug", "service_invoicegen", "Processing usage items for generic_with_usage");
                                /*
                                	Usage Item Basics
                                */
                                // start the item
                                $invoice_item = new invoice_items();
                                $invoice_item->id_invoice = $invoiceid;
                                $invoice_item->type_invoice = "ar";
                                $invoice_item->type_item = "service_usage";
                                $itemdata = array();
                                $itemdata["chartid"] = $obj_service->data["chartid"];
                                $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " usage from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"];
                                $itemdata["customid"] = $obj_service->id;
                                $itemdata["discount"] = 0;
                                /*
                                	Adjust Included Units to handle partial or extended periods
                                */
                                if ($ratio != "1") {
                                    $obj_service->data["included_units"] = sprintf("%d", $obj_service->data["included_units"] * $ratio);
                                }
                                /*
                                	Fetch usage amount
                                */
                                $usage_obj = new service_usage_generic();
                                $usage_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $usage_obj->date_start = $period_usage_data["date_start"];
                                $usage_obj->date_end = $period_usage_data["date_end"];
                                if ($usage_obj->load_data_service()) {
                                    $usage_obj->fetch_usagedata();
                                    if ($usage_obj->data["total_byunits"]) {
                                        $usage = $usage_obj->data["total_byunits"];
                                    } else {
                                        $usage = $usage_obj->data["total"];
                                    }
                                }
                                unset($usage_obj);
                                /*
                                	Charge for the usage in units
                                */
                                $unitname = addslashes($obj_service->data["units"]);
                                if ($usage > $obj_service->data["included_units"]) {
                                    // there is excess usage that we can bill for.
                                    $usage_excess = $usage - $obj_service->data["included_units"];
                                    // set item attributes
                                    $itemdata["price"] = $obj_service->data["price_extraunits"];
                                    $itemdata["quantity"] = $usage_excess;
                                    $itemdata["units"] = $unitname;
                                    // description example:		Used 120 ZZ out of 50 ZZ included in plan
                                    //				Excess usage of 70 ZZ charged at $5.00 per ZZ
                                    $itemdata["description"] .= "\nUsed {$usage} {$unitname} out of " . $obj_service->data["included_units"] . " {$unitname} included in plan.";
                                    $itemdata["description"] .= "\nExcess usage of {$usage_excess} {$unitname} charged at " . $obj_service->data["price_extraunits"] . " per {$unitname}.";
                                } else {
                                    // description example:		Used 120 ZZ out of 50 ZZ included in plan
                                    $itemdata["description"] .= "\nUsed {$usage} {$unitname} out of " . $obj_service->data["included_units"] . " {$unitname} included in plan.";
                                }
                                /*
                                	Add the item to the invoice
                                */
                                $invoice_item->prepare_data($itemdata);
                                $invoice_item->action_update();
                                unset($invoice_item);
                                /*
                                	Update usage value for period - this summary value is visable on the service
                                	history page and saves having to query lots of records to generate period totals.
                                */
                                $sql_obj = new sql_query();
                                $sql_obj->string = "UPDATE services_customers_periods SET usage_summary='{$usage}' WHERE id='" . $period_usage_data["id"] . "' LIMIT 1";
                                $sql_obj->execute();
                                break;
                            case "licenses":
                                /*
                                	LICENSES
                                
                                	No data usage, but there is a quantity field for the customer's account to specify the
                                	quantity of licenses that they have.
                                */
                                log_write("debug", "service_invoicegen", "Processing usage items for licenses");
                                /*
                                	Usage Item Basics
                                */
                                // start the item
                                $invoice_item = new invoice_items();
                                $invoice_item->id_invoice = $invoiceid;
                                $invoice_item->type_invoice = "ar";
                                $invoice_item->type_item = "service_usage";
                                $itemdata = array();
                                $itemdata["chartid"] = $obj_service->data["chartid"];
                                $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " usage from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"];
                                $itemdata["customid"] = $obj_service->id;
                                $itemdata["discount"] = 0;
                                /*
                                	Determine Additional Charges
                                */
                                // charge for any extra licenses
                                if ($obj_service->data["quantity"] > $obj_service->data["included_units"]) {
                                    // there is excess usage that we can bill for.
                                    $licenses_excess = $obj_service->data["quantity"] - $obj_service->data["included_units"];
                                    // set item attributes
                                    $itemdata["price"] = $obj_service->data["price_extraunits"] * $ratio;
                                    $itemdata["quantity"] = $licenses_excess;
                                    $itemdata["units"] = addslashes($obj_service->data["units"]);
                                    // description example:		10 licences included
                                    //				2 additional licenses charged at $24.00 each
                                    $itemdata["description"] .= "\n" . $obj_service->data["included_units"] . " " . $obj_service->data["units"] . " included";
                                    $itemdata["description"] .= "\n{$licenses_excess} additional " . $obj_service->data["units"] . " charged at " . $obj_service->data["price_extraunits"] . " each.";
                                } else {
                                    // description example:		10 licenses
                                    $itemdata["description"] .= "\n" . $period_data["quantity"] . " " . $period_data["units"] . ".";
                                }
                                /*
                                	Add the item to the invoice
                                */
                                $invoice_item->prepare_data($itemdata);
                                $invoice_item->action_update();
                                unset($invoice_item);
                                break;
                            case "time":
                                /*
                                	TIME
                                
                                	Simular to the generic usage type, but instead of units being a text field, units
                                	is an ID to the service_units table.
                                */
                                log_write("debug", "service_invoicegen", "Processing usage items for time traffic");
                                /*
                                	Usage Item Basics
                                */
                                // start the item
                                $invoice_item = new invoice_items();
                                $invoice_item->id_invoice = $invoiceid;
                                $invoice_item->type_invoice = "ar";
                                $invoice_item->type_item = "service_usage";
                                $itemdata = array();
                                $itemdata["chartid"] = $obj_service->data["chartid"];
                                $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " usage from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"];
                                $itemdata["customid"] = $obj_service->id;
                                $itemdata["discount"] = 0;
                                /*
                                	Adjust Included Units to handle partial or extended periods
                                */
                                if ($ratio != "1") {
                                    $obj_service->data["included_units"] = sprintf("%d", $obj_service->data["included_units"] * $ratio);
                                }
                                /*
                                	Fetch usage amount
                                */
                                $usage_obj = new service_usage_generic();
                                $usage_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $usage_obj->date_start = $period_usage_data["date_start"];
                                $usage_obj->date_end = $period_usage_data["date_end"];
                                if ($usage_obj->load_data_service()) {
                                    $usage_obj->fetch_usagedata();
                                    if ($usage_obj->data["total_byunits"]) {
                                        $usage = $usage_obj->data["total_byunits"];
                                    } else {
                                        $usage = $usage_obj->data["total"];
                                    }
                                }
                                unset($usage_obj);
                                /*
                                	Charge for the usage in units
                                */
                                $unitname = sql_get_singlevalue("SELECT name as value FROM service_units WHERE id='" . $obj_service->data["units"] . "'");
                                if ($usage > $obj_service->data["included_units"]) {
                                    // there is excess usage that we can bill for.
                                    $usage_excess = $usage - $obj_service->data["included_units"];
                                    // set item attributes
                                    $itemdata["price"] = $obj_service->data["price_extraunits"];
                                    $itemdata["quantity"] = $usage_excess;
                                    $itemdata["units"] = $unitname;
                                    // description example:		Used 120 GB out of 50 GB included in plan
                                    //				Excess usage of 70 GB charged at $5.00 per GB
                                    $itemdata["description"] .= "\nUsed {$usage} {$unitname} out of " . $obj_service->data["included_units"] . " {$unitname} included in plan.";
                                    $itemdata["description"] .= "\nExcess usage of {$usage_excess} {$unitname} charged at " . $obj_service->data["price_extraunits"] . " per {$unitname}.";
                                } else {
                                    // description example:		Used 10 out of 50 included units
                                    $itemdata["description"] .= "\nUsed {$usage} {$unitname} out of " . $obj_service->data["included_units"] . " {$unitname} included in plan.";
                                }
                                /*
                                	Add the item to the invoice
                                */
                                $invoice_item->prepare_data($itemdata);
                                $invoice_item->action_update();
                                unset($invoice_item);
                                /*
                                	Update usage value for period - this summary value is visable on the service
                                	history page and saves having to query lots of records to generate period totals.
                                */
                                $sql_obj = new sql_query();
                                $sql_obj->string = "UPDATE services_customers_periods SET usage_summary='{$usage}' WHERE id='" . $period_usage_data["id"] . "' LIMIT 1";
                                $sql_obj->execute();
                                break;
                            case "data_traffic":
                                /*
                                	DATA_TRAFFIC
                                
                                	We make use of the service_usage_traffic logic to determine the usage across all IP
                                	addressess assigned to this customer and then bill accordingly.
                                */
                                log_write("debug", "service_invoicegen", "Processing usage items for time/data_traffic");
                                /*
                                	Fetch data traffic plan usage type
                                */
                                $unitname = sql_get_singlevalue("SELECT name as value FROM service_units WHERE id='" . $obj_service->data["units"] . "'");
                                /*
                                	Fetch usage amount - the returned usage structure will include breakdown of traffic
                                	by configured types.
                                */
                                $usage_obj = new service_usage_traffic();
                                $usage_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $usage_obj->date_start = $period_usage_data["date_start"];
                                $usage_obj->date_end = $period_usage_data["date_end"];
                                /*
                                	Fetch Traffic Caps & Details
                                
                                	Returns all the traffic cap types including overrides.
                                
                                	id_type,
                                	id_cap,
                                	type_name,
                                	type_label,
                                	cap_mode,
                                	cap_units_included,
                                	cap_units_price
                                */
                                $traffic_types_obj = new traffic_caps();
                                $traffic_types_obj->id_service = $obj_service->id;
                                $traffic_types_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $traffic_types_obj->load_data_traffic_caps();
                                $traffic_types_obj->load_data_override_caps();
                                /*
                                	Generate Traffic Bills
                                */
                                if ($usage_obj->load_data_service()) {
                                    $usage_obj->fetch_usage_traffic();
                                    foreach ($traffic_types_obj->data as $data_traffic_cap) {
                                        // Adjust Included Units to handle partial or extended periods
                                        if ($ratio != "1") {
                                            $data_traffic_cap["cap_units_included"] = sprintf("%d", $data_traffic_cap["cap_units_included"] * $ratio);
                                        }
                                        // if there is only a single traffic cap, we should make the traffic type name blank, since there's only going to be
                                        // one line item anyway.
                                        if ($traffic_types_obj->data_num_rows == 1) {
                                            $data_traffic_cap["type_name"] = "";
                                        }
                                        // if the any traffic type is zero and there are other traffic types, we should skip it, since most likely
                                        // the other traffic types provide everything expected.
                                        //
                                        if ($traffic_types_obj->data_num_rows > 1 && $data_traffic_cap["type_label"] == "*" && $usage_obj->data["total_byunits"]["*"] == 0) {
                                            continue;
                                        }
                                        // start service item
                                        $invoice_item = new invoice_items();
                                        $invoice_item->id_invoice = $invoiceid;
                                        $invoice_item->type_invoice = "ar";
                                        $invoice_item->type_item = "service_usage";
                                        $itemdata = array();
                                        $itemdata["chartid"] = $obj_service->data["chartid"];
                                        $itemdata["customid"] = $obj_service->id;
                                        // base details
                                        $itemdata["price"] = 0;
                                        $itemdata["quantity"] = 0;
                                        $itemdata["discount"] = 0;
                                        $itemdata["units"] = "";
                                        $itemdata["description"] = addslashes($obj_service->data["name_service"]) . " usage from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"];
                                        if ($data_traffic_cap["cap_mode"] == "unlimited") {
                                            // unlimited data cap, there will never be any excess traffic charges, so the line item
                                            // description should be purely for informative purposes.
                                            $itemdata["description"] .= "\nUnlimited " . addslashes($data_traffic_cap["type_name"]) . " traffic, total of " . $usage_obj->data["total_byunits"][$data_traffic_cap["type_label"]] . " {$unitname} used\n";
                                        } else {
                                            // capped traffic - check for excess changes, otherwise just report on how much traffic
                                            // that the customer used.
                                            // description example:		Used 10 GB out of 50 GB included in plan
                                            $itemdata["description"] .= "\nCapped " . addslashes($data_traffic_cap["type_name"]) . " traffic, used " . $usage_obj->data["total_byunits"][$data_traffic_cap["type_label"]] . " {$unitname} out of " . $data_traffic_cap["cap_units_included"] . " {$unitname} in plan.";
                                            // handle excess charges
                                            //
                                            if ($usage_obj->data["total_byunits"][$data_traffic_cap["type_label"]] > $data_traffic_cap["cap_units_included"]) {
                                                // there is excess usage that we can bill for.
                                                $usage_excess = $usage_obj->data["total_byunits"][$data_traffic_cap["type_label"]] - $data_traffic_cap["cap_units_included"];
                                                // set item attributes
                                                $itemdata["price"] = $data_traffic_cap["cap_units_price"];
                                                $itemdata["quantity"] = $usage_excess;
                                                $itemdata["units"] = $unitname;
                                                // description example:		Excess usage of 70 GB charged at $5.00 per GB
                                                $itemdata["description"] .= "\nExcess usage of {$usage_excess} {$unitname} charged at " . format_money($data_traffic_cap["cap_units_price"]) . " per {$unitname}.";
                                            }
                                        }
                                        // end of traffic cap mode
                                        // add trunk usage item
                                        //
                                        $invoice_item->prepare_data($itemdata);
                                        $invoice_item->action_update();
                                        unset($invoice_item);
                                    }
                                }
                                /*
                                	Update usage value for period - this summary value is visable on the service
                                	history page and saves having to query lots of records to generate period totals.
                                */
                                $sql_obj = new sql_query();
                                $sql_obj->string = "UPDATE services_customers_periods SET usage_summary='" . $usage_obj->data["total_byunits"]["total"] . "' WHERE id='" . $period_usage_data["id"] . "' LIMIT 1";
                                $sql_obj->execute();
                                unset($usage_obj);
                                unset($traffic_types_obj);
                                break;
                            case "phone_single":
                            case "phone_tollfree":
                            case "phone_trunk":
                                /*
                                	PHONE_* SERVICES
                                
                                	The phone services are special and contain multiple usage items, for:
                                	* Additional DDI numbers
                                	* Additional Trunks
                                
                                	There are also multiple items for the call charges, grouped into one
                                	item for each DDI.
                                */
                                log_write("debug", "service_invoicegen", "Processing usage items for phone_single/phone_tollfree/phone_trunk");
                                // setup usage object
                                $usage_obj = new service_usage_cdr();
                                $usage_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $usage_obj->date_start = $period_usage_data["date_start"];
                                $usage_obj->date_end = $period_usage_data["date_end"];
                                $usage_obj->load_data_service();
                                /*
                                	1. DDI CHARGES
                                
                                	We need to fetch the total number of DDIs and see if there are any excess
                                	charges due to overage of the allocated amount in the plan.
                                */
                                if ($obj_service->data["typeid_string"] == "phone_trunk" && $period_data["mode"] == "standard") {
                                    // start service item
                                    $invoice_item = new invoice_items();
                                    $invoice_item->id_invoice = $invoiceid;
                                    $invoice_item->type_invoice = "ar";
                                    $invoice_item->type_item = "service_usage";
                                    $itemdata = array();
                                    $itemdata["chartid"] = $obj_service->data["chartid"];
                                    $itemdata["customid"] = $obj_service->id;
                                    $itemdata["discount"] = 0;
                                    // fetch DDI usage
                                    $usage = $usage_obj->load_data_ddi();
                                    // determine excess usage charges
                                    if ($usage > $obj_service->data["phone_ddi_included_units"]) {
                                        // there is excess usage that we can bill for.
                                        $usage_excess = $usage - $obj_service->data["phone_ddi_included_units"];
                                        // set item attributes
                                        $itemdata["price"] = $obj_service->data["phone_ddi_price_extra_units"] * $ratio;
                                        log_write("debug", "DEBUG", "Ratio is {$ratio}, price is " . $itemdata["price"] . "");
                                        $itemdata["quantity"] = $usage_excess;
                                        $itemdata["units"] = "DDIs";
                                        if ($obj_service->data["phone_ddi_included_units"]) {
                                            $itemdata["description"] = $obj_service->data["phone_ddi_included_units"] . "x DDI numbers included in service plan plus additional " . $usage_excess . "x numbers from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                        } else {
                                            // no included units, we use an alternative string format
                                            $itemdata["description"] = $usage_excess . "x DDI numbers from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                        }
                                    } else {
                                        // no charge for this item
                                        $itemdata["description"] = $obj_service->data["phone_ddi_included_units"] . "x DDI numbers included in service plan from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                    }
                                    // add trunk usage item
                                    $invoice_item->prepare_data($itemdata);
                                    $invoice_item->action_update();
                                    unset($invoice_item);
                                }
                                /*
                                	2. Trunk Charges
                                */
                                if (($obj_service->data["typeid_string"] == "phone_trunk" || $obj_service->data["typeid_string"] == "phone_tollfree") && $period_data["mode"] == "standard") {
                                    // fetch the number of trunks included in the plan, along with the number provided
                                    // we can then see if there are any excess charges for these
                                    // start service item
                                    $invoice_item = new invoice_items();
                                    $invoice_item->id_invoice = $invoiceid;
                                    $invoice_item->type_invoice = "ar";
                                    $invoice_item->type_item = "service_usage";
                                    $itemdata = array();
                                    $itemdata["chartid"] = $obj_service->data["chartid"];
                                    $itemdata["customid"] = $obj_service->id;
                                    $itemdata["discount"] = 0;
                                    // determine excess usage charges
                                    if ($obj_service->data["phone_trunk_quantity"] > $obj_service->data["phone_trunk_included_units"]) {
                                        // there is excess usage that we can bill for.
                                        $usage_excess = $obj_service->data["phone_trunk_quantity"] - $obj_service->data["phone_trunk_included_units"];
                                        // set item attributes
                                        $itemdata["price"] = $obj_service->data["phone_trunk_price_extra_units"] * $ratio;
                                        $itemdata["quantity"] = $usage_excess;
                                        $itemdata["units"] = "trunks";
                                        if ($obj_service->data["phone_trunk_included_units"]) {
                                            $itemdata["description"] = $obj_service->data["phone_trunk_included_units"] . "x trunks included in service plan plus additional " . $usage_excess . "x trunks from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                        } else {
                                            // no included trunks, adjust string to suit.
                                            $itemdata["description"] = $usage_excess . "x trunks from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                        }
                                    } else {
                                        // no charge for this item
                                        $itemdata["description"] = $obj_service->data["phone_trunk_included_units"] . "x trunks included in service plan from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                    }
                                    // add trunk usage item
                                    $invoice_item->prepare_data($itemdata);
                                    $invoice_item->action_update();
                                    unset($invoice_item);
                                }
                                /*
                                									Call Charges
                                	Use CDR usage billing module to handle call charges.
                                */
                                $usage_obj = new service_usage_cdr();
                                $usage_obj->id_service_customer = $period_usage_data["id_service_customer"];
                                $usage_obj->date_start = $period_usage_data["date_start"];
                                $usage_obj->date_end = $period_usage_data["date_end"];
                                $billgroup_obj = new sql_query();
                                $billgroup_obj->string = "SELECT id, billgroup_name FROM cdr_rate_billgroups";
                                $billgroup_obj->execute();
                                $billgroup_obj->fetch_array();
                                if ($usage_obj->load_data_service()) {
                                    $usage_obj->fetch_usage_calls();
                                    foreach ($billgroup_obj->data as $data_billgroup) {
                                        foreach ($usage_obj->data_ddi as $ddi) {
                                            if ($usage_obj->data[$ddi][$data_billgroup["id"]]["charges"] > 0) {
                                                // start service item
                                                $invoice_item = new invoice_items();
                                                $invoice_item->id_invoice = $invoiceid;
                                                $invoice_item->type_invoice = "ar";
                                                $invoice_item->type_item = "service_usage";
                                                $itemdata = array();
                                                $itemdata["chartid"] = $obj_service->data["chartid"];
                                                $itemdata["customid"] = $obj_service->id;
                                                // extra service details
                                                $itemdata["id_service_customer"] = $period_usage_data["id_service_customer"];
                                                $itemdata["id_period"] = $period_usage_data["id"];
                                                // determine excess usage charges
                                                $itemdata["discount"] = 0;
                                                $itemdata["price"] = $usage_obj->data[$ddi][$data_billgroup["id"]]["charges"];
                                                $itemdata["quantity"] = "1";
                                                $itemdata["units"] = "";
                                                $itemdata["description"] = $data_billgroup["billgroup_name"] . " call charges for {$ddi} from " . $period_usage_data["date_start"] . " to " . $period_usage_data["date_end"] . "";
                                                $itemdata["cdr_billgroup"] = $data_billgroup["id"];
                                                // add trunk usage item
                                                $invoice_item->prepare_data($itemdata);
                                                $invoice_item->action_update();
                                                unset($invoice_item);
                                            } else {
                                                log_write("debug", "inc_service_invoicegen", "Excluding DDI {$ddi} from " . $data_billgroup["billgroup_name"] . " due to no charges for the current period");
                                            }
                                        }
                                    }
                                }
                                unset($usage_obj);
                                /*
                                	If enabled, generate the CDR output format for the current service usage and attach
                                	it to the invoice via the invoice journal.
                                
                                	This feature can be enabled/disabled on a per-customer per-service basis.
                                */
                                if ($obj_service->data["billing_cdr_csv_output"]) {
                                    log_write("debug", "inc_service_invoicegen", "Generating CDR export file and attaching to invoice journal");
                                    // generate the CSV formatted export.
                                    $cdr_options = array('id_customer' => $customer_data["id"], 'id_service_customer' => $period_usage_data["id_service_customer"], 'period_start' => $period_usage_data["date_start"], 'period_end' => $period_usage_data["date_end"]);
                                    $csv = new cdr_csv($cdr_options);
                                    if (!($cdr_output = $csv->getCSV())) {
                                        log_write("error", "inc_service_invoicegen", "Unable to generate CSV ouput for the configured range");
                                        return 0;
                                    }
                                    // create journal entry
                                    $journal = new journal_process();
                                    $journal->prepare_set_journalname("account_ar");
                                    $journal->prepare_set_customid($invoiceid);
                                    $journal->prepare_set_type("file");
                                    // we use the prefix "SERVICE:" to find the journal at invoice time
                                    $journal->prepare_set_title("SERVICE: Service CDR Export Attachment");
                                    // details can be anything (just a text block)
                                    $data["content"] = NULL;
                                    $data["content"] .= "Automatically exported CDR for service " . addslashes($obj_service->data["name_service"]) . "\n";
                                    $data["content"] .= "\n";
                                    $journal->prepare_set_content($data["content"]);
                                    $journal->action_update();
                                    // create journal entry
                                    $journal->action_lock();
                                    // lock entry to avoid users deleting it or breaking it
                                    // upload file as an attachment for the journal
                                    $file_obj = new file_storage();
                                    $file_obj->data["type"] = "journal";
                                    $file_obj->data["customid"] = $journal->structure["id"];
                                    $file_obj->data["file_name"] = "invoice_" . $invoicecode . "_service_CDR_export.csv";
                                    if (!$file_obj->action_update_var($cdr_output)) {
                                        log_write("error", "inc_service_invoicegen", "Unable to upload export CDR invoice to journal.");
                                    }
                                    unset($csv);
                                    unset($journal);
                                    unset($file_obj);
                                    unset($cdr_output);
                                }
                                break;
                            case "generic_no_usage":
                            case "bundle":
                                // nothing todo for these service types
                                log_write("debug", "service_invoicegen", "Not processing usage, this is a non-usage service type");
                                break;
                            default:
                                // we should always match all service types, even if we don't need to do anything
                                // in particular for that type.
                                die("Unable to process unknown service type: " . $obj_service->data["typeid_string"] . "");
                                break;
                        }
                        // end of processing usage
                    } else {
                        log_write("debug", "service_invoicegen", "Not billing for current usage, as this appears to be the first plan period so no usage can exist yet");
                    }
                    /*
                    	Set invoice ID for period - this prevents the period from being added to
                    	any other invoices and allows users to see which invoice it was billed under
                    */
                    // set for plan period
                    $sql_obj = new sql_query();
                    $sql_obj->string = "UPDATE services_customers_periods SET invoiceid='{$invoiceid}', rebill='0' WHERE id='" . $period_data["id"] . "' LIMIT 1";
                    $sql_obj->execute();
                    // set for usage period
                    if (!empty($period_usage_data["active"])) {
                        $sql_obj = new sql_query();
                        $sql_obj->string = "UPDATE services_customers_periods SET invoiceid_usage='{$invoiceid}' WHERE id='" . $period_usage_data["id"] . "' LIMIT 1";
                        $sql_obj->execute();
                    }
                }
                // end of processing periods
                /*
                	Only process orders and invoice
                	summary details if we had no errors above.
                */
                if (!error_check()) {
                    /*
                    	Process any customer orders
                    */
                    if ($GLOBALS["config"]["ORDERS_BILL_ONSERVICE"]) {
                        log_write("debug", "inc_service_invoicegen", "Checking for customer orders to add to service invoice");
                        $obj_customer_orders = new customer_orders();
                        $obj_customer_orders->id = $customer_data["id"];
                        if ($obj_customer_orders->check_orders_num()) {
                            log_write("debug", "inc_service_invoicegen", "Order items exist, adding them to service invoice");
                            $obj_customer_orders->invoice_generate($invoiceid);
                        }
                    } else {
                        log_write("debug", "inc_service_invoicegen", "Not checking for customer orders, ORDERS_BILL_ONSERVICE is disabled currently");
                    }
                    /*
                    	Update the invoice details + Ledger
                    
                    	Processes:
                    	- taxes
                    	- ledger
                    	- invoice summary
                    
                    	We use the invoice_items class to perform these tasks, but we don't need
                    	to define an item ID for the functions being used to work.
                    */
                    $invoice = new invoice_items();
                    $invoice->id_invoice = $invoiceid;
                    $invoice->type_invoice = "ar";
                    $invoice->action_update_tax();
                    $invoice->action_update_ledger();
                    $invoice->action_update_total();
                    unset($invoice);
                    /*
                    	Update period information with invoiceid
                    */
                    $sql_obj = new sql_query();
                    $sql_obj->string = "UPDATE services_customers_periods SET invoiceid='{$invoiceid}' WHERE id='" . $period_data["id"] . "'";
                    $sql_obj->execute();
                }
                // end if error check
                /*
                	Automatic Payments
                
                	Makes automatic invoice payments using sources such as customer credit pools, reoccuring credit card transactions
                	and other sources.
                */
                if ($GLOBALS["config"]["ACCOUNTS_AUTOPAY"]) {
                    log_write("debug", "inc_services_invoicegen", "Autopay Functionality Enabled, running appropiate functions for invoice ID {$invoiceid}");
                    $obj_autopay = new invoice_autopay();
                    $obj_autopay->id_invoice = $invoiceid;
                    $obj_autopay->type_invoice = "ar";
                    $obj_autopay->autopay();
                    unset($obj_autopay);
                }
                /*
                	Commit
                
                	Conduct final error check, before commiting the new invoice and sending the customer an email
                	if appropiate.
                
                	(we obviously don't want to email them if the invoice is getting rolled back!)
                */
                $sql_obj = new sql_query();
                if (error_check()) {
                    $sql_obj->trans_rollback();
                    log_write("error", "inc_services_invoicegen", "An error occured whilst creating service invoice. No changes have been made.");
                } else {
                    $sql_obj->trans_commit();
                    // invoice creation complete - remove any notifications made by the invoice functions and return
                    // our own notification
                    $_SESSION["notification"]["message"] = array();
                    log_write("notification", "inc_services_invoicegen", "New invoice {$invoicecode} for customer " . $customer_data["code_customer"] . " created");
                    /*
                    		Send the invoice to the customer as a PDF via email
                    */
                    $emailed = "unsent";
                    if (sql_get_singlevalue("SELECT value FROM config WHERE name='EMAIL_ENABLE'") == "enabled") {
                        if (sql_get_singlevalue("SELECT value FROM config WHERE name='ACCOUNTS_INVOICE_AUTOEMAIL'") == "enabled") {
                            // load completed invoice data
                            $invoice = new invoice();
                            $invoice->id = $invoiceid;
                            $invoice->type = "ar";
                            $invoice->load_data();
                            $invoice->load_data_export();
                            if ($invoice->data["amount_total"] > 0) {
                                // generate an email
                                $email = $invoice->generate_email();
                                // send email
                                $invoice->email_invoice("system", $email["to"], $email["cc"], $email["bcc"], $email["subject"], $email["message"]);
                                // complete
                                log_write("notification", "inc_services_invoicegen", "Invoice {$invoicecode} has been emailed to customer (" . $email["to"] . ")");
                            } else {
                                // complete - invoice is for $0, so don't want to email out
                                log_write("notification", "inc_services_invoicegen", "Invoice {$invoicecode} has not been emailed to the customer due to invoice being for \$0.");
                            }
                            $emailed = "emailed - " . $email["to"];
                            unset($invoice);
                        }
                    }
                    // end if email enabled
                }
                // end if commit successful
                /*
                	Review Status
                
                	Here we need to check whether invoicing succeded/failed for the selected customer and process accordingly - we
                	also want to re-set the error flag if running a batch mode, to enable other customers to still be invoiced.
                */
                if (error_check()) {
                    // an error occured
                    $invoice_stats["total_failed"]++;
                    $invoice_stats["failed"][] = array("code_customer" => $customer_data["code_customer"], "name_customer" => $customer_data["name_customer"], "code_invoice" => $invoicecode);
                    // clear the error strings if we are processing from CLI this will allow us to continue on
                    // with additional invoices.
                    if (!empty($_SESSION["mode"])) {
                        if ($_SESSION["mode"] == "cli") {
                            log_write("debug", "inc_services_invoicegen", "Processing from CLI, clearing error flag and continuing with additional invoices");
                            error_clear();
                        }
                    }
                } else {
                    // successful
                    $invoice_stats["total"]++;
                    $invoice_stats["generated"][] = array("code_customer" => $customer_data["code_customer"], "name_customer" => $customer_data["name_customer"], "code_invoice" => $invoicecode, "sent" => $emailed);
                }
                // end of success/stats review
            }
            // end of processing customers
        }
        // end of if customers exist
    } else {
        log_debug("inc_services_invoicegen", "No services assigned to customer {$customerid}");
    }
    /*
    	Write Invoicing Report
    	
    	This only takes place if no customer ID is provided, eg we have run a full automatic invoice generation report.
    */
    if ($customerid == NULL) {
        log_write("debug", "inc_service_invoicegen", "Generating invoice report for invoice generation process");
        /*
        	Invoice Stats Calculations
        */
        $invoice_stats["time"] = time() - $invoice_stats["time_start"];
        if ($invoice_stats["time"] == 0) {
            $invoice_stats["time"] = 1;
        }
        /*
        	Write Invoice Report
        */
        $invoice_report = array();
        $invoice_report[] = "Complete Invoicing Run Time:\t" . $invoice_stats["time"] . " seconds";
        $invoice_report[] = "Total Invoices Generated:\t" . $invoice_stats["total"];
        $invoice_report[] = "Failed Invoices/Customers:\t" . $invoice_stats["total_failed"];
        if (isset($invoice_stats["generated"])) {
            $invoice_report[] = "";
            $invoice_report[] = "Customers / Invoices Generated";
            foreach (array_keys($invoice_stats["generated"]) as $id) {
                $invoice_report[] = " * " . $invoice_stats["generated"][$id]["code_invoice"] . ": " . $invoice_stats["generated"][$id]["code_customer"] . " -- " . $invoice_stats["generated"][$id]["name_customer"];
                $invoice_report[] = "   [" . $invoice_stats["generated"][$id]["sent"] . "]";
            }
            $invoice_report[] = "";
        }
        if (isset($invoice_stats["failed"])) {
            $invoice_report[] = "";
            $invoice_report[] = "Failed Customers / Invoices";
            foreach (array_keys($invoice_stats["failed"]) as $id) {
                $invoice_report[] = " * " . $invoice_stats["failed"][$id]["code_customer"] . " -- " . $invoice_stats["failed"][$id]["name_customer"];
            }
            $invoice_report[] = "";
            $invoice_report[] = "Failed invoices will be attempted again on the following billing run.";
            $invoice_report[] = "";
        }
        $invoice_report[] = "Invoicing Run Complete";
        // display to debug log
        log_write("debug", "inc_service_invoicegen", "----");
        foreach ($invoice_report as $line) {
            // loop through invoice report lines
            log_write("debug", "inc_service_invoicegen", $line);
        }
        log_write("debug", "inc_service_invoicegen", "----");
        // email if appropiate
        if ($GLOBALS["config"]["ACCOUNTS_INVOICE_BATCHREPORT"] && ($invoice_stats["total"] > 0 || $invoice_stats["total_failed"] > 0)) {
            log_write("debug", "inc_service_invoicegen", "Emailing invoice generation report to " . $GLOBALS["config"]["ACCOUNTS_EMAIL_ADDRESS"] . "");
            /*
            	External dependency of Mail_Mime
            */
            if (!@(include_once 'Mail.php')) {
                log_write("error", "invoice", "Unable to find Mail module required for sending email");
                break;
            }
            if (!@(include_once 'Mail/mime.php')) {
                log_write("error", "invoice", "Unable to find Mail::Mime module required for sending email");
                break;
            }
            /*
            	Email the Report
            */
            $email_sender = $GLOBALS["config"]["ACCOUNTS_EMAIL_ADDRESS"];
            $email_to = $GLOBALS["config"]["ACCOUNTS_EMAIL_ADDRESS"];
            $email_subject = "Invoice Batch Process Report";
            $email_message = $GLOBALS["config"]["COMPANY_NAME"] . "\n\nInvoice Batch Process Report for " . time_format_humandate() . "\n\n";
            foreach ($invoice_report as $line) {
                $email_message .= $line . "\n";
            }
            // prepare headers
            $mail_headers = array('From' => $email_sender, 'Subject' => $email_subject);
            $mail_mime = new Mail_mime("\n");
            $mail_mime->setTXTBody($email_message);
            $mail_body = $mail_mime->get();
            $mail_headers = $mail_mime->headers($mail_headers);
            $mail =& Mail::factory('mail');
            $status = $mail->send($email_to, $mail_headers, $mail_body);
            if (PEAR::isError($status)) {
                log_write("error", "inc_service_invoicegen", "An error occured whilst attempting to send the batch report email: " . $status->getMessage() . "");
            } else {
                log_write("debug", "inc_service_invoicegen", "Successfully sent batch report email.");
            }
        }
        // end if email
    }
    // end if report
    return 1;
}