<?php


function run_manual_sript($script, $path_to_root)
{
    
    
    $access_code = $_POST['access_code'];
    if(validate_admin_functions_access_code($access_code) !== true){
        display_error("Access code entered is not valid.");
        return;
    }

    switch ($script) {
        case 'Update_Item_Standard_Cost_Script':
            include_once($path_to_root . "/inventory/includes/db/items_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_item_std_cost.inc");
            import_system_item_material_cost();
            break;
        
        case 'custom_script_copy_boq_group_boq_item':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_copy_boq_group_boq_item.inc");
            include_once($path_to_root . "/admin/db/list_values_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
            include_once($path_to_root . "/projects/includes/db/projects_db.inc");            
            break;
        
        case 'custom_script_copy_budgets_to_boq_items':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_copy_budgets_to_boq_items.inc");
            include_once($path_to_root . "/admin/db/list_values_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
            include_once($path_to_root . "/projects/includes/db/projects_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_boq_item_costs_db.inc");
            include_once($path_to_root . "/projects/includes/db/project_boq_item_detail_edit_db.inc");
            copy_task_groups_to_boq_items();
            break;
        
        case 'update_purchase_order_tax_group':
            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_purchase_order_tax_group.inc");
            cs_update_purchase_order_tax_group();
            break;

        case 'update_purchase_order_tax_details':
            include_once($path_to_root . "/purchasing/includes/po_class.inc");
            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_purchase_order_tax_details.inc");
            cs_update_purchase_order_tax_details();
            break;
        
        case 'update_sales_order_tax_details':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_sales_order_tax_details.inc");
            cs_update_sales_order_tax_details();
            break;
        
        case 'write_debtor_trans_tax_groups':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_debtor_trans_tax_group.inc");
            cs_write_debtor_trans_tax_groups();
            break;
        
        case 'update_supplier_invoice_tax_group':
            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_supplier_invoice_tax_group.inc");
            cs_update_supplier_invoice_tax_group();
            break;

        case 'update_iou_batch_tran_date':
            include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_expenses_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_iou_batch_tran_date.inc");
            cs_update_iou_batch_tran_date();
            break;

        case 'update_project_invoice_line_categories':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_project_invoice_line_category.inc");
            include_once($path_to_root . "/projects/includes/db/project_invoice_db.inc");
            include_once($path_to_root . "/admin/db/list_values_db.inc");
            cs_update_project_invoice_line_categories();
            break;
        
        case 'update_loan_payment_loan_id':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_company_loan_payment_loan_id.inc");
            include_once($path_to_root . "/finance/includes/db/company_loan_installments_db.inc");
            cs_update_loan_payment_loan_id();
            break;
        

        case 'update_project_approved_budgets_total':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_project_approved_budgets_total.inc");
            include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
            cs_update_project_approved_budgets_total();
            break;
        
        case 'write_sales_orders_tax_groups':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_sales_order_tax_group.inc");            
            cs_write_sales_orders_tax_groups();
            break;
        
        case 'update_customer_credit_note_header_src':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_customer_credit_note_header_src.inc");            
            cs_update_customer_credit_note_header_src();
            break;
        
        case 'update_pr_issues_header_total':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_pr_issues_header_total.inc");            
            cs_update_custom_script_update_pr_issues_header_total();
            break;
        
        case 'update_iou_paid_total':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_iou_paid_total.inc");            
            include_once($path_to_root . "/petty_cash/includes/db/petty_cash_payments_db.inc");  
            cs_custom_script_update_iou_paid_total();
            break;
        
        case 'generate_project_expenses_schedule':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_generate_project_expenses_schedule.inc");            
            include_once($path_to_root . "/admin/db/months_db.inc");            
            include_once($path_to_root . "/projects/includes/db/projects_expenses_schedule_db.inc");            
            cs_generate_project_expenses_schedule();
            break;

        case 'generate_project_billing_schedule':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_generate_project_billing_schedule.inc");            
            include_once($path_to_root . "/admin/db/months_db.inc");            
            include_once($path_to_root . "/projects/includes/db/projects_billing_schedule_db.inc"); 
            include_once($path_to_root . "/admin/db/list_values_db.inc");
            cs_generate_project_billing_schedule();
            break;
        
        case 'update_pr_issue_total':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_pr_issue_total.inc");            
            include_once($path_to_root . "/purchasing/includes/db/pr_issues_db.inc");  
            cs_custom_script_update_pr_issue_total();
            break;
        
        case 'cs_update_customer_allocations':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_customer_allocations.inc");            
            include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");  
            cs_custom_script_update_customer_allocations();
            break;

        case 'update_pr_line_total_issued_qty':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_pr_line_total_issued_qty.inc");            
            include_once($path_to_root . "/purchasing/includes/db/pr_issues_db.inc");
//            include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
            cs_update_pr_line_total_issued_qty();
            break;

        case 'update_pr_allocations':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_pr_allocations.inc");            
            include_once($path_to_root . "/purchasing/includes/db/pr_issues_db.inc");
            include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
            include_once($path_to_root . "/includes/project_cost_functions.inc");
            cs_update_pr_allocations();
            break;
        
        case 'update_task_allocation_totals':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_task_allocation_totals.inc");            
            include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
            cs_update_task_allocation_totals();
            break;
        
        case 'create_float_reimbursement_details':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_float_reimbursement_details.inc");            
            include_once($path_to_root . "/petty_cash/includes/db/petty_cash_float_reimbursement_inquiry_db.inc");  
            include_once($path_to_root . "/petty_cash/includes/db/petty_cash_float_reimbursement_db.inc");  

            cs_create_float_reimbursement_details();
            break;
        
        case 'update_petty_cash_float_balances':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_update_petty_cash_float_balance.inc");            
            cs_update_petty_cash_float_balances();
            break;
        
        case 'non_stock_to_stock_grn_details':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_non_stock_to_stock_grn_details.inc");            
            cs_custom_script_non_stock_to_stock_grn_details();
            break;
        
        case 'create_non_stock_moves':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_non_stock_moves.inc");            
            cs_custom_script_create_non_stock_moves();
            break;

        case 'create_supplier_payment_gl_entries':
            include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc");
            include_once($path_to_root . "/gl/includes/db/gl_db_bank_trans.inc");
            include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");
            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_supplier_payment_gl_entries.inc");
            cs_create_supplier_payment_gl_entries();
            break;
        
        case 'create_float_reimbursement_gl_entries':
//            include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc");
//            include_once($path_to_root . "/gl/includes/db/gl_db_bank_trans.inc");
//            include_once($path_to_root . "/gl/includes/db/gl_db_bank_accounts.inc");
//            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
//            include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_float_reimbursement_gl_entries.inc");
            cs_create_float_reimbursement_gl_entries();
            break;
        
        case 'create_stax_fi_entries':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_stax_fi_entries.inc");
            include_once($path_to_root . "/gl/includes/fi/ui/generate_fi_accounting_entries_ui.inc");
            include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            include_once($path_to_root . "/gl/includes/fi/db/generate_fi_accounting_entries_db.inc");
            cs_create_stax_fi_entries();
            break;
        
        case 'post_stax_fi_entries':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_post_stax_fi_entries.inc"); 
            include_once($path_to_root . "/gl/includes/fi/db/post_fi_accounting_entries_db.inc"); 
            include_once($path_to_root . "/gl/includes/db/gl_db_trans.inc"); 
            cs_post_stax_fi_entries();
            break;
        
        case 'create_batch_item_issue_to_stock_moves':
            include_once($path_to_root . "/admin/includes/custom_scripts/custom_script_create_batch_item_issue_to_stock_moves.inc");            
            include_once($path_to_root . "/includes/types.inc");            
            include_once($path_to_root . "/includes/db/inventory_db.inc");            
            cs_custom_script_create_batch_item_issue_to_stock_moves();
            break;

        case '':
            display_error("No script selected");
            break;
        default :
            display_error("Unknown script");
            break;
    }


}



function write_custom_script_log($log_filename, $message="")
{

    global $installed_path;
      
    $log_filename .= ".txt"; 
    $log_filepathname = $installed_path . '/tmp/custom_script_logs/'.$log_filename;
    file_put_contents($log_filepathname, format_microtime('Y-m-d H:i:s.u')."\r\n",FILE_APPEND);
    file_put_contents($log_filepathname, $message."\r\n"."\r\n",FILE_APPEND);
            
}


function cs_display_run_custom_script_edit_controls()
{
    $values_array['Update_Item_Standard_Cost_Script'] = 'Update_Item_Standard_Cost_Script';
    $values_array['custom_script_copy_boq_group_boq_item'] = 'custom_script_copy_boq_group_boq_item';
    $values_array['custom_script_copy_budgets_to_boq_items'] = 'custom_script_copy_budgets_to_boq_items';
    $values_array['update_iou_payment_float_id'] = 'Update IOU Payment Float Id';
    $values_array['update_purchase_order_tax_group'] = 'Update Purchase Order Tax Group';
    $values_array['update_purchase_order_tax_details'] = 'Update Purchase Order Tax Details';
    $values_array['update_supplier_invoice_tax_group'] = 'Update Supplier Invoice Tax Group';
    $values_array['update_iou_batch_tran_date'] = 'Update IOU Batch Tran Date';
    $values_array['update_project_invoice_line_categories'] = 'Update Project Invoice Line Category';
    $values_array['update_loan_payment_loan_id'] = 'Update Loan Payment Loan Id';
    $values_array['update_project_approved_budgets_total'] = 'Update project approved budgets total';

    start_table(TABLESTYLE); 
    //custom_values_list_row("Script", 'custom_script', $values_array, null, "");
    text_row("Script Name:", 'custom_script', null, 100, 200);
    password_row(_("Access Code:"), 'access_code', '');
    
    end_table();
    
}