function write_sets($_POST)
{
    global $catids, $CUBIT_MODULES;
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($smonth, "num", 1, 2, "Invalid Financial year starting month.");
    /*$v->isOk ($monthend,"num",1 ,2, "Invalid month end date");
    	$v->isOk ($int1,"float",1 ,5, "Invalid interest 1.");
    	$v->isOk ($int2,"float",1 ,5, "Invalid interest 2.");
    	$v->isOk ($int3,"float",1 ,5, "Invalid interest 3.");
    	$v->isOk ($brack1,"float",1 ,10, "Invalid bracket 1.");
    	$v->isOk ($brack2,"float",1 ,10, "Invalid bracket 2.");*/
    # display errors, if any
    if ($v->isError()) {
        $confirmCust = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirmCust .= "<li class='err'>" . $e["msg"] . "</li>";
        }
        return $confirmCust;
    }
    pglib_transaction("BEGIN");
    makemap($smonth);
    core_connect();
    $sql = "SELECT accname FROM accounts WHERE accnum != '999' AND div = '" . USER_DIV . "'";
    $Rslt = db_exec($sql) or errDie("Unable to check database for existing accounts.");
    if (pg_numrows($Rslt) > 0) {
        $acc = "\n\t\t\t\t\t<center>\n\t\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<td><li class='err'>ERROR : There are already accounts in Cubit</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Note : </th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t\t<td>Quick Setup can only be run on a new cubit installation.</td>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t</table>\n\t\t\t\t\t<p>\n\t\t\t\t\t<table " . TMPL_tblDflts . " width='15%'>\n\t\t\t\t\t\t" . TBL_BR . "\n\t\t\t\t\t\t<tr>\n\t\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t\t</tr>\n\t\t\t\t\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t\t\t\t\t</table>";
        return $acc;
    }
    $sql = "\n\t\tINSERT INTO cubit.compinfo (\n\t\t\tcompname, slogan, logoimg, addr1, addr2, addr3, \n\t\t\taddr4, paddr1, paddr2, paddr3, tel, fax, \n\t\t\tvatnum, regnum, imgtype, img, div, paye, \n\t\t\tterms, postcode, img2, imgtype2, logoimg2, diplomatic_indemnity\n\t\t) VALUES (\n\t\t\t'{$_SESSION['comp']}', '', '', '', '', '', \n\t\t\t'', '', '', '', '', '',\n\t\t\t'', '', '', '', '" . USER_DIV . "', '', \n\t\t\t'', '', '', '', '', 'N'\n\t\t);";
    db_exec($sql) or errDie("Unable to update company information.");
    db_conn('cubit');
    $sql = "SELECT label FROM set WHERE label = 'ACCNEW_LNK' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to check database for existing account creation settings.");
    if (pg_num_rows($rslt) > 0) {
        $sql = "\n\t\t\tUPDATE set \n\t\t\tSET value = 'acc-new2.php', type = 'Account Creation' \n\t\t\tWHERE label = 'ACCNEW_LNK'";
    } else {
        $sql = "\n\t\t\tINSERT INTO set (\n\t\t\t\ttype, label, value, \n\t\t\t\tdescript, div\n\t\t\t) VALUES (\n\t\t\t\t'Account Creation', 'ACCNEW_LNK', 'acc-new2.php', \n\t\t\t\t'Use user selected account numbers', '" . USER_DIV . "'\n\t\t\t)";
    }
    db_exec($sql) or errDie("Unable to insert account creation settings to Cubit.");
    /* account categories */
    $catids = array("I" => 0, "E" => 0, "B" => 0);
    $sql = "\n\t\tINSERT INTO core.income (\n\t\t\tcatid, catname, div\n\t\t) VALUES (\n\t\t\t'I' || nextval('core.income_seq'), 'Income', '" . USER_DIV . "'\n\t\t)";
    $catRslt = db_exec($sql) or errDie("Unable to add income Category to Database.");
    $catids["I"] = "I" . pglib_getlastid("core.income_seq");
    $sql = "\n\t\tINSERT INTO core.expenditure (\n\t\t\tcatid, catname, div\n\t\t) VALUES (\n\t\t\t'E' || nextval('core.expenditure_seq'),'Expenditure', '" . USER_DIV . "'\n\t\t)";
    $catRslt = db_exec($sql) or errDie("Unable to add expense Category to Database.");
    $catids["E"] = "E" . pglib_getlastid("core.expenditure_seq");
    $sql = "\n\t\tINSERT INTO core.balance (\n\t\t\tcatid, catname, div\n\t\t) VALUES (\n\t\t\t'B' || nextval('core.balance_seq'),'Balance', '" . USER_DIV . "'\n\t\t)";
    $catRslt = db_exec($sql) or errDie("Unable to add balance Category to Database.");
    $catids["B"] = "B" . pglib_getlastid("core.balance_seq");
    /* START SETUP */
    $catid = $catids["I"];
    $sales_account = newacc("1000", "000", "Sales", "I", "f", "sales");
    $pos_sales_account = newacc("1100", "000", "Point of Sale - Sales", "I", "f", "sales");
    newacc("1150", "000", "Interest Received", "I", "f");
    newacc("1200", "000", "Sundry Income", "I", "f");
    newacc("1250", "000", "Exchange Rate Profit/Loss", "I", "f", "other_income");
    newacc("1300", "000", "Sale of Assets", "I", "f", "sales");
    linkacc("1300", "000", "salesacc", "saleofassets");
    linkacc("1150", "000", "salacc", "interestreceived");
    newacc("1660", "000", "Creditors Settlement Discount", "I", "f");
    newacc("1995", "000", "Previous Year Adjustment Income 1", "I", "f");
    newacc("1996", "000", "Previous Year Adjustment Income 2", "I", "f");
    newacc("1997", "000", "Previous Year Adjustment Income 3", "I", "f");
    newacc("1998", "000", "Previous Year Adjustment Income 4", "I", "f");
    newacc("1999", "000", "Previous Year Adjustment Income 5", "I", "f");
    $catid = $catids["E"];
    $cost_account = newacc("2150", "000", "Cost of Sales", "E", "f", "cost_of_sales");
    $pension_account = newacc("2510", "000", "Pension", "E", "f");
    newacc("2520", "000", "Retirement Annuity Fund", "E", "f");
    linkacc("2520", "000", "salacc", "retireexpense");
    newacc("2530", "000", "Provident Fund", "E", "f");
    linkacc("2530", "000", "salacc", "providentexpense");
    newacc("2540", "000", "Medical Aid", "E", "f");
    linkacc("2540", "000", "salacc", "medicalexpense");
    newacc("2160", "000", "Cost Variance", "E", "f", "cost_of_sales");
    linkacc("2160", "000", "pchsacc", "Cost Variance");
    newacc("2170", "000", "Variance", "E", "f", "cost_of_sales");
    linkacc("2170", "000", "salesacc", "sales_variance");
    newacc("2500", "000", "Salaries and Wages", "E", "f");
    linkacc("2500", "000", "salacc", "salaries");
    newacc("2550", "000", "Salaries - Commission", "E", "f");
    linkacc("2550", "000", "salacc", "Commission");
    newacc("2555", "000", "Salaries - Bonus", "E", "f");
    linkacc("2555", "000", "salacc", "Bonus");
    newacc("2560", "000", "UIF", "E", "f");
    linkacc("2560", "000", "salacc", "uifexp");
    newacc("2570", "000", "SDL", "E", "f");
    linkacc("2570", "000", "salacc", "sdlexp");
    newacc("2000", "000", "Accounting Fees", "E", "f");
    newacc("2050", "000", "Advertising and Promotions", "E", "f");
    newacc("2100", "000", "Bank Charges", "E", "f");
    newacc("2200", "000", "Depreciation", "E", "f");
    newacc("2250", "000", "Electricity and Water", "E", "f");
    newacc("2300", "000", "General Expenses", "E", "f");
    newacc("2350", "000", "Insurance", "E", "f");
    newacc("2400", "000", "Interest Paid", "E", "f");
    newacc("2450", "000", "Printing and Stationery", "E", "f");
    newacc("2650", "000", "Rent Paid", "E", "f");
    newacc("2600", "000", "Telephone and Fax", "E", "f");
    newacc("2700", "000", "POS Rounding", "E", "f", "cost_of_sales");
    linkacc("2700", "000", "salesacc", "rounding");
    newacc("2800", "000", "Normal Tax", "E", "f", "tax");
    linkacc("2510", "000", "salacc", "pensionexpense");
    newacc("2660", "000", "Creditors Settlement Discount", "E", "f");
    newacc("3660", "000", "Debtors Settlement Discount", "E", "f");
    newacc("4995", "000", "Previous Year Adjustment Expense 1", "E", "f");
    newacc("4996", "000", "Previous Year Adjustment Expense 2", "E", "f");
    newacc("4997", "000", "Previous Year Adjustment Expense 3", "E", "f");
    newacc("4998", "000", "Previous Year Adjustment Expense 4", "E", "f");
    newacc("4999", "000", "Previous Year Adjustment Expense 5", "E", "f");
    $catid = $catids["B"];
    newacc("5200", "000", "Retained Income / Accumulated Loss", "B", "f", "retained_income");
    newacc("5250", "000", "Share Capital / Members Contribution", "B", "f", "share_capital");
    newacc("5300", "000", "Shareholder / Director / Members Loan Account", "B", "f", "shareholders_loan");
    newacc("6000", "000", "Land & Buildings - Net Value", "B", "f", "fixed_asset");
    newacc("6000", "010", "Land & Buildings - Cost", "B", "f", "fixed_asset");
    newacc("6000", "020", "Land & Buildings - Accum Depreciation", "B", "f", "fixed_asset");
    newacc("6100", "020", "Motor Vehicle - Accum Depreciation", "B", "f", "fixed_asset");
    newacc("6100", "000", "Motor Vehicle - Net Value", "B", "f", "fixed_asset");
    newacc("6100", "010", "Motor Vehicle - Cost", "B", "f", "fixed_asset");
    newacc("6150", "000", "Computer Equipment - Net Value", "B", "f", "fixed_asset");
    newacc("6150", "010", "Computer Equipment - Cost", "B", "f", "fixed_asset");
    newacc("6150", "020", "Computer Equipment - Accum Depreciation", "B", "f", "fixed_asset");
    newacc("6160", "000", "Office Equipment - Net Value", "B", "f", "fixed_asset");
    newacc("6160", "010", "Office Equipment - Cost", "B", "f", "fixed_asset");
    newacc("6160", "020", "Office Equipment - Accum Depreciation", "B", "f", "fixed_asset");
    newacc("6170", "000", "Furniture & Fittings - Net Value", "B", "f", "fixed_asset");
    newacc("6170", "010", "Furniture & Fittings - Cost", "B", "f", "fixed_asset");
    newacc("6170", "020", "Furniture & Fittings - Accum Depreciation", "B", "f", "fixed_asset");
    $stock_control = newacc("6300", "000", "Inventory Suspense Account", "B", "f", "current_asset");
    $stock_account = newacc("6350", "000", "Inventory", "B", "f", "current_asset");
    $deptors_account = newacc("6400", "000", "Customer Control Account", "B", "f", "current_asset");
    $creditors_account = newacc("6500", "000", "Supplier Control Account", "B", "f", "current_liability");
    newacc("6600", "000", "Employees Control Account", "B", "f", "current_liability");
    newacc("2151", "000", "Stock Take Suspense Account", "E", "f", "cost_of_sales");
    linkacc("6600", "000", "salacc", "salaries control");
    linkacc("6600", "000", "salacc", "salaries control original");
    newacc("6700", "000", "Employee Loan Account", "B", "f", "current_asset");
    linkacc("6700", "000", "salacc", "loanacc");
    $bank_account = newacc("7000", "000", "Bank", "B", "f", "current_asset");
    newacc("7100", "000", "Petty Cash", "B", "f", "current_asset");
    linkacc("7100", "000", "bankacc", "Petty Cash");
    $pos_cash_account = newacc("7200", "000", "Cash on Hand", "B", "f", "current_asset");
    linkacc("7200", "000", "salacc", "cash");
    newacc("7300", "000", "POS Credit Card Control", "B", "f", "current_asset");
    linkacc("7300", "000", "salacc", "cc");
    newacc("8000", "000", "VAT Control Account", "B", "f", "current_liability");
    linkacc("8000", "000", "salesacc", "VAT");
    newacc("8010", "000", "VAT Input Account", "B", "f", "current_liability");
    linkacc("8010", "000", "salesacc", "VATIN");
    newacc("8020", "000", "VAT Output Account", "B", "f", "current_liability");
    linkacc("8020", "000", "salesacc", "VATOUT");
    newacc("8100", "000", "PAYE Payable", "B", "f", "current_liability");
    linkacc("8100", "000", "salacc", "PAYE");
    newacc("8200", "000", "UIF Payable", "B", "f", "current_liability");
    linkacc("8200", "000", "salacc", "UIF");
    linkacc("8200", "000", "salacc", "uifbal");
    newacc("8300", "000", "SDL Payable", "B", "f", "current_liability");
    linkacc("8300", "000", "salacc", "sdlbal");
    newacc("8400", "000", "Pension Payable", "B", "f", "current_liability");
    linkacc("8400", "000", "salacc", "pension");
    newacc("8500", "000", "Medical Aid Payable", "B", "f", "current_liability");
    linkacc("8500", "000", "salacc", "medical");
    newacc("8600", "000", "Retirement Annuity Fund Payable", "B", "f", "current_liability");
    linkacc("8600", "000", "salacc", "retire");
    newacc("8700", "000", "Provident Fund Payable", "B", "f", "current_liability");
    linkacc("8700", "000", "salacc", "provident");
    newacc("9000", "000", "Opening Balances / Suspense Account", "B", "f", "current_liability");
    newacc("9995", "000", "Previous Year Adjustment Balance 1", "B", "f", "fixed_asset");
    newacc("9996", "000", "Previous Year Adjustment Balance 2", "B", "f", "fixed_asset");
    newacc("9997", "000", "Previous Year Adjustment Balance 3", "B", "f", "fixed_asset");
    newacc("9998", "000", "Previous Year Adjustment Balance 4", "B", "f", "fixed_asset");
    newacc("9999", "000", "Previous Year Adjustment Balance 5", "B", "f", "fixed_asset");
    # Check if year has been opened
    $sql = "DELETE FROM core.year";
    $rslt = db_exec($sql);
    for ($i = 1; $i <= 10; $i++) {
        $sql = "INSERT INTO core.year VALUES('y" . ($selyear + $i - 1) . "', 'yr{$i}', 'n', '" . USER_DIV . "')";
        $rslt = db_exec($sql) or errDie("Could not set year name in Cubit", SELF);
    }
    $yrname = "y{$selyear}";
    $endmon = $smonth - 1;
    if (intval($endmon) == 0) {
        $endmon = 12;
    }
    $Sql = "TRUNCATE core.range";
    $Rs = db_exec($Sql) or errDie("Unable to empty year range", SELF);
    $firstmonth = $smonth;
    $activeyear = $yrname;
    $sql = "\n\t\tINSERT INTO core.range (\n\t\t\t\"start\", \"end\", div\n\t\t) VALUES (\n\t\t\t'{$smonth}', '{$endmon}', '" . USER_DIV . "'\n\t\t)";
    $Rslt = db_exec($sql) or errDie("Unable to insert year range", SELF);
    $sql = "SELECT * FROM core.year WHERE yrname='{$yrname}'";
    $yrs = db_exec($sql);
    $yr = pg_fetch_array($yrs);
    if ($yr['closed'] == 'y') {
        return "<center><li class='err'>ERROR : The Selected Financial year : <b>{$yrname}</b> has been closed.\n\t\t<p><input type='button' onClick='JavaScript:history.back();' value='&laquo; Correct submission'>";
    }
    $yrdb = $yr['yrdb'];
    $sql = "SELECT * FROM core.range";
    $Rslt = db_exec($sql);
    if (pg_numrows($Rslt) < 1) {
        $OUTPUT = "<center><li class='err'>ERROR : The Financial year Period range was not found on Database, Please make sure that everything is set during instalation.</li>";
        require "template.php";
    }
    $range = Pg_fetch_array($Rslt);
    // Months array
    $months = array("dummy", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    $sql = "INSERT INTO core.active (yrdb, yrname, prddb, prdname, div) VALUES ('{$yrdb}', '{$yrname}', '{$range['start']}', '" . $months[$range['start']] . "', '" . USER_DIV . "')";
    $rslt = db_exec($sql) or errDie("Could not Set Next Year Database and Name", SELF);
    db_conn("exten");
    $sql = "INSERT INTO departments (deptno, deptname, incacc, debtacc, credacc, pia, pca, div) VALUES ('1', 'Ledger 1', '{$sales_account}', '{$deptors_account}', '{$creditors_account}', '{$pos_sales_account}', '{$pos_cash_account}', '" . USER_DIV . "')";
    $deptRslt = db_exec($sql) or errDie("Unable to add deparment to system.", SELF);
    $sql = "INSERT INTO salespeople (salespno, salesp, div) VALUES ('1', 'General', '" . USER_DIV . "')";
    $salespRslt = db_exec($sql) or errDie("Unable to add warehouse to system.", SELF);
    $sql = "INSERT INTO  categories (category, div) VALUES ('General', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add category to system.", SELF);
    $sql = "INSERT INTO  class (classname, div) VALUES ('General', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add fringe benefit to system.", SELF);
    $sql = "INSERT INTO warehouses (whno, whname, stkacc, cosacc, conacc, div) VALUES ('1', 'Store 1', '{$stock_account}', '{$cost_account}', '{$stock_control}', '" . USER_DIV . "')";
    $whouseRslt = db_exec($sql) or errDie("Unable to add warehouse to system.", SELF);
    $whid = pglib_lastid("warehouses", "whid");
    $sql = "INSERT INTO  pricelist (listname, div) VALUES ('Standard', '" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to price list to system.", SELF);
    $sql = "INSERT INTO cubit.stockcat (catcod, cat, descript, div) VALUES('1', 'General', 'General Stock Category', '" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert stock category to Cubit.", SELF);
    $sql = "INSERT INTO cubit.stockclass (classcode, classname, div) VALUES ('1', 'General', '" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to add class to system.", SELF);
    $sql = "SELECT label FROM cubit.set WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
    if (pg_num_rows($rslt) > 0) {
        $sql = "UPDATE cubit.set SET value = '{$whid}', type = 'Default Warehouse' WHERE label = 'DEF_WH' AND div = '" . USER_DIV . "'";
    } else {
        $sql = "INSERT INTO cubit.set (type, label, value, descript, div) VALUES('Default Warehouse', 'DEF_WH', '{$whid}', '1 &nbsp;&nbsp;&nbsp; Store1', '" . USER_DIV . "')";
    }
    db_exec($sql) or errDie("Unable to insert settings to Cubit.");
    $sql = "SELECT label FROM cubit.set WHERE label = 'SELAMT_VAT' AND div = '" . USER_DIV . "'";
    $rslt = db_exec($sql) or errDie("Unable to check database for existing settings.");
    if (pg_num_rows($rslt) > 0) {
        $sql = "UPDATE cubit.set SET value = 'inc', descript = 'Vat Inclusive' WHERE label = 'SELAMT_VAT' AND div = '" . USER_DIV . "'";
    } else {
        $sql = "INSERT INTO cubit.set (type, label, value, descript, div) VALUES('Vat type on stock selling price', 'SELAMT_VAT', 'inc', 'Vat Inclusive', '" . USER_DIV . "')";
    }
    db_exec($sql) or errDie("Unable to insert settings to Cubit.");
    $sql = "INSERT INTO cubit.currency (symbol,curcode,descrip,rate,def) VALUES ('R','ZAR', 'Rand',0.00,'')";
    db_exec($sql) or errDie("Unable to insert currency.");
    $sql = "\n\t\tINSERT INTO cubit.bankacct (\n\t\t\tacctype, bankname, branchname, branchcode, accname, \n\t\t\taccnum, details, div, btype, \n\t\t\tfcid, currency\n\t\t) VALUES (\n\t\t\t'Cheque', 'Bank', 'Branch', '000000', 'Account Name', \n\t\t\t'000000000000', 'Default bank Account', '" . USER_DIV . "', 'loc', \n\t\t\t(SELECT fcid FROM cubit.currency WHERE curcode='ZAR' LIMIT 1), 'Rand'\n\t\t)";
    db_exec($sql) or errDie("Unable to add bank account to database.");
    $accid = pglib_lastid("cubit.bankacct", "bankid");
    $sql = "INSERT INTO cubit.set (type, label, value, descript, div)\n\t\t\tVALUES('Banking Details Account', 'BANK_DET', '3', 'Bank Account: Account Name - Bank', '" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to set default bank account.");
    $hook = "INSERT INTO core.bankacc (accid, accnum, div) VALUES('{$accid}', '{$bank_account}', '" . USER_DIV . "')";
    $Rlst = db_exec($hook) or errDie("Unable to add link for for new bank account", SELF);
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Add Client','../customers-new.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Client','../customers-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('New Invoice','../cust-credit-stockinv.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Find Invoice','../invoice-search.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Stock','../stock-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Add Supplier','../supp-new.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Suppliers','../supp-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('New Purchase','../purchase-new.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Purchases','../purchase-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Add Quote','../quote-new.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Invoices','../invoice-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('View Quotes','../quote-view.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Debtors Age Analysis','../reporting/debt-age-analysis.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Creditors Age Analysis','../reporting/cred-age-analysis.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.links (name,script) VALUES ('Bank Reconciliation','../reporting/bank-recon.php')";
    db_exec($sql) or errDie("Unable to insert link.");
    $sql = "INSERT INTO crm.teams (name,div) VALUES ('Sales','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.teams (name,div) VALUES ('Support','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.teams (name,div) VALUES ('Accounts','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.teams (name,div) VALUES ('Company Relations','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.teams (name,div) VALUES ('Purchasing - Supplier Relations','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Product Enquiries','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Place an Order','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Complain','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Account querries','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Delivery or Installation Tracking','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Comment on good service or Remarks','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Ask about employment','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('General','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Potential Supplier','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.tcats (name,div) VALUES ('Product Support','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into teams");
    $sql = "INSERT INTO crm.actions (action) VALUES ('Called - Need to call again.')";
    db_exec($sql) or errDie("Unable to insert action.");
    $sql = "INSERT INTO crm.actions (action) VALUES ('Called - Could not get in touch')";
    db_exec($sql) or errDie("Unable to insert action.");
    $sql = "INSERT INTO crm.actions (action) VALUES ('Requested more information')";
    db_exec($sql) or errDie("Unable to insert action.");
    $sql = "INSERT INTO crm.actions (action) VALUES ('Sent Fax')";
    db_exec($sql) or errDie("Unable to insert action.");
    $pactivemonth = $activemonth;
    $pactivemonth--;
    if ($pactivemonth == 0) {
        $pactivemonth = 12;
    }
    $i = 0;
    $current = $firstmonth;
    $current--;
    if ($current == 0) {
        $current = 12;
    }
    /* disabled, it wurks differently now */
    while ($current != $pactivemonth && 0) {
        $i++;
        if ($i > 20) {
            break;
        }
        $current++;
        if ($current == 13) {
            $current = 1;
        }
        close_month('yr1', $current);
    }
    for ($i = 1; $i <= 12; ++$i) {
        close_month('yr1', $i);
    }
    $sql = "SELECT accid FROM core.accounts WHERE accname='Bank Charges'";
    $rslt = db_exec($sql);
    $ad = pg_fetch_array($rslt);
    $bc = $ad['accid'];
    $sql = "SELECT accid FROM core.accounts WHERE accname='Interest Paid'";
    $rslt = db_exec($sql);
    $ad = pg_fetch_array($rslt);
    $i = $ad['accid'];
    $sql = "SELECT accid FROM core.accounts WHERE accname='Interest Received'";
    $rslt = db_exec($sql);
    $ad = pg_fetch_array($rslt);
    $ii = $ad['accid'];
    $sql = "INSERT INTO exten.spricelist (listname,div) VALUES ('Standard','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert into supplier price list.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('CASH DEPOSIT FEE','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('FEE CHEQUE CASHED','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('FEE-SPECIAL PRESENTATION','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('SERVICE FEE','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('OVERDRAFT LEDGER FEE','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('INTEREST','i','-','c','{$i}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('INTEREST','i','+','c','{$ii}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('TRANSACTION CHARGE ','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('ADMIN CHARGE','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('GARAGE CRD CHARGES','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('STAMP DUTY','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('BANKING CHARGES','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.statement_refs (ref,dets,pn,action,account,by) VALUES ('01 CASH DEP','i','-','c','{$bc}','Default');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "CREATE INDEX stkid_stock_key ON cubit.stock USING btree(stkid);";
    db_exec($sql) or errDie("Unable to index.");
    $sql = "CREATE INDEX accid_accounts_key ON core.accounts USING btree(accid);";
    db_exec($sql) or errDie("Unable to index.");
    $sql = "CREATE INDEX accid_trial_bal_key ON core.trial_bal USING btree(accid);";
    db_exec($sql) or errDie("Unable to index.");
    $sql = "INSERT INTO cubit.vatcodes (code,description,del,zero,vat_amount) VALUES ('01','Normal','Yes','No','14');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.vatcodes (code,description,del,zero,vat_amount) VALUES ('02','Capital Goods','No','No','14');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.vatcodes (code,description,del,zero,vat_amount) VALUES ('03','Capital Goods','No','Yes','0');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.vatcodes (code,description,del,zero,vat_amount) VALUES ('04','Zero VAT','No','Yes','0');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.vatcodes (code,description,del,zero,vat_amount) VALUES ('05','VAT Exempt','No','Yes','0');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Disciplinary Verbal Warning','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Disciplinary Written Warning','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Dismissal','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Corrective Counselling','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Performance Counselling','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Grievance','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Disputes Mediation','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Disputes Conciliation','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.report_types (type,div) VALUES ('Disputes Arbitration','" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.login_retries (tries, minutes) VALUES ('0', '0');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.supp_groups (id, groupname) VALUES ('0', '[None]');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.template_settings (template, filename, div) VALUES ('statements', 'pdf/pdf-statement.php', '" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.template_settings (template, filename, div) VALUES ('invoices', 'invoice-print.php', '" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.template_settings (template, filename, div) VALUES ('reprints', 'new', '" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.workshop_settings (setting, value, div) VALUES ('workshop_conditions', 'As per display notice.', '" . USER_DIV . "');";
    db_exec($sql) or errDie("Unable to insert data.");
    $sql = "INSERT INTO cubit.set(type, label, value, descript, div) VALUES('Block main accounts', 'BLOCK', 'use', 'Block main accounts', '" . USER_DIV . "')";
    db_exec($sql) or errDie("Error setting up default setting.");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('0','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('7','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('14','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('30','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('60','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('90','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.ct (days,div) VALUES ('120','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('0','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('7','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('14','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('30','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('60','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('90','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    $sql = "INSERT INTO exten.od (days,div) VALUES ('120','" . USER_DIV . "')";
    db_exec($sql) or errDie("Unable to insert default terms");
    if (is_readable("setup-ratios.php")) {
        include "setup-ratios.php";
    }
    if (!isset($inst_mode) or strlen($inst_mode) < 1) {
        $inst_mode = "hq";
    }
    #record the install type ...
    $sql = "\n\t\tINSERT INTO cubit.settings (\n\t\t\tconstant, label, value, type, datatype, \n\t\t\tminlen, maxlen, div, readonly\n\t\t) VALUES (\n\t\t\t'INST_MODE', 'Cubit Install Mode', '{$inst_mode}', 'company', 'allstring', \n\t\t\t'1', '250', '0', 'f'\n\t\t);";
    db_exec($sql) or errDie("Unable to insert install mode.");
    /* run the addon setups */
    foreach ($CUBIT_MODULES as $modulename) {
        if (is_readable("{$modulename}/setup-addon.php")) {
            include "{$modulename}/setup-addon.php";
        }
    }
    db_conn('core');
    block();
    pglib_transaction("COMMIT");
    $sets = "\n\t\t\t\t<table " . TMPL_tblDflts . " width='50%'>\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Setup Complete</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td>Cubit is ready to be used.</td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>\n\t\t\t\t<p>\n\t\t\t\t<table " . TMPL_tblDflts . ">\n\t\t\t\t\t<tr>\n\t\t\t\t\t\t<th>Quick Links</th>\n\t\t\t\t\t</tr>\n\t\t\t\t\t<tr class='" . bg_class() . "'>\n\t\t\t\t\t\t<td><a href='main.php'>Main Menu</a></td>\n\t\t\t\t\t</tr>\n\t\t\t\t</table>";
    return $sets;
}
function createcat($catname, $div, $type)
{
    core_connect();
    # In case no upper case
    $type = strtoupper($type);
    switch ($type) {
        case "I":
            $tab = "income";
            break;
        case "B":
            $tab = "balance";
            break;
        case "E":
            $tab = "expenditure";
            break;
        default:
            return "<li> Invalid Category type";
    }
    # Make seq
    $seq = $tab . "_seq";
    # Insert Category
    $sql = "INSERT INTO {$tab} (catid, catname, div) VALUES ('{$type}' || nextval('{$seq}'), '{$catname}', '{$div}')";
    $catRslt = db_exec($sql) or errDie("Unable to add Category to Database.");
    # Get last inserted id for new cat
    $catid = pglib_getlastid("{$seq}");
    $catid = $type . $catid;
    return $catid;
}
function write($_POST)
{
    # get vars
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($depid, "num", 1, 50, "Invalid Department ID.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class='err'>-" . $e["msg"] . "</li>";
        }
        return $confirm;
    }
    # begin sql transaction
    core_connect();
    pglib_transaction("BEGIN") or errDie("Unable to start a database transaction.", SELF);
    // Create income accounts
    # Create a Default Category
    $seq = "Income_seq";
    # Write to db
    $sql = "INSERT INTO income (catid, catname, div) VALUES ('I' || nextval('{$seq}'), 'INCOME', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add Category to Database.");
    # get last inserted id for new cat
    $inccatid = pglib_getlastid("income_seq");
    $inccatid = "I" . $inccatid;
    # Query server for income account
    $i = 0;
    $sql = "SELECT * FROM defacc WHERE depid = '{$depid}' AND topacc >= " . MIN_INC . " AND topacc <= " . MAX_INC . " ORDER BY topacc, accnum ASC";
    $accRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
    if (pg_numrows($accRslt) < 1) {
        return "<li>There are no income accounts.</li>";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        if (create($acc['topacc'], $acc['accnum'], $acc['accname'], $inccatid, "I", "f") > 0) {
            pglib_transaction("ROLLBACK");
            return "<li> Failed To return accounts ({$acc['topacc']}, {$acc['accnum']}, {$acc['accname']}).";
        }
        $i++;
    }
    // Create Expenditure accounts
    # Create a Default Category
    $seq = "expenditure_seq";
    # Write to db
    $sql = "INSERT INTO expenditure (catid, catname, div) VALUES ('E' || nextval('{$seq}'), 'EXPENDITURE', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add Category to Database.");
    # get last inserted id for new cat
    $expcatid = pglib_getlastid("expenditure_seq");
    $expcatid = "E" . $expcatid;
    # Query server for income account
    $i = 0;
    $sql = "SELECT * FROM defacc WHERE depid = '{$depid}' AND topacc >= " . MIN_EXP . " AND topacc <= " . MAX_EXP . " ORDER BY topacc, accnum ASC";
    $accRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
    if (pg_numrows($accRslt) < 1) {
        return "<li>There are no income accounts.</li>";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        if (create($acc['topacc'], $acc['accnum'], $acc['accname'], $expcatid, "E", "f") > 0) {
            pglib_transaction("ROLLBACK");
            return "<li> Failed To return accounts ({$acc['topacc']}, {$acc['accnum']}, {$acc['accname']}).";
        }
        $i++;
    }
    // Create Balance accounts
    # Create a Default Category
    $seq = "balance_seq";
    # Write to db
    $sql = "INSERT INTO balance (catid, catname, div) VALUES ('B' || nextval('{$seq}'), 'BALANCE', '" . USER_DIV . "')";
    $catRslt = db_exec($sql) or errDie("Unable to add Category to Database.");
    # get last inserted id for new cat
    $balcatid = pglib_getlastid("balance_seq");
    $balcatid = "B" . $balcatid;
    # Query server for income account
    $i = 0;
    $sql = "SELECT * FROM defacc WHERE depid = '{$depid}' AND topacc >= " . MIN_BAL . " AND topacc <= " . MAX_BAL . " ORDER BY topacc, accnum ASC";
    $accRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
    if (pg_numrows($accRslt) < 1) {
        return "<li>There are no income accounts.";
    }
    while ($acc = pg_fetch_array($accRslt)) {
        if (create($acc['topacc'], $acc['accnum'], $acc['accname'], $balcatid, "B", "f") > 0) {
            pglib_transaction("ROLLBACK");
            return "<li> Failed To return accounts ({$acc['topacc']}, {$acc['accnum']}, {$acc['accname']}).";
        }
        $i++;
    }
    # commit sql transaction
    pglib_transaction("COMMIT") or errDie("Unable to start a database transaction.", SELF);
    # Query server
    core_connect();
    $sql = "SELECT * FROM defdep WHERE depid = '{$depid}'";
    $depRslt = db_exec($sql) or errDie("Unable to retrieve stocks from database.");
    if (pg_numrows($depRslt) < 1) {
        return "<li> Invalid Company Type ID.</li>";
    }
    $dep = pg_fetch_array($depRslt);
    # Block setting
    db_connect();
    $sql = "INSERT INTO set(type, label, value, descript, div) VALUES('Default accounts', 'DEF_ACC', '{$dep['depname']}', 'Default accounts', '" . USER_DIV . "')";
    $setRslt = db_exec($sql) or errDie("Unable to set settings in Cubit.");
    return "\n\t<center>\n\t<h3>Company Type Default Accounts</h3>\n\t<table " . TMPL_tblDflts . ">\n\t\t<tr class='" . bg_class() . "'>\n\t\t\t<td align='center'><li>All accounts have been created</td>\n\t\t</tr>\n\t</table>\n\t<p>\n\t<table " . TMPL_tblDflts . " width='15%'>\n        <tr><td><br></td></tr>\n        <tr>\n        \t<th>Quick Links</th>\n        </tr>\n\t\t<script>document.write(getQuicklinkSpecial());</script>\n\t</table>";
}