<?php
/**
 * phpaga
 *
 * add company
 *
 * Add a company
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: add_company.php 837 2007-08-26 21:35:19Z florian $
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

include_once ("./config.php");

$status = "";
$new_cpn_id = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

    $cpnInfo["cpn_name"] = $_POST["cpn_name"];
    $cpnInfo["cpn_address"] = $_POST["cpn_address"];
    $cpnInfo["cpn_address2"] = $_POST["cpn_address2"];
    $cpnInfo["cpn_city"] = $_POST["cpn_city"];
    $cpnInfo["cpn_region"] = $_POST["cpn_region"];
    $cpnInfo["cpn_zip"] = $_POST["cpn_zip"];
    $cpnInfo["ctr_id"] = $_POST["ctr_id"];
    $cpnInfo["ccat_id"] = $_POST["ccat_id"];
    $cpnInfo["cpn_taxnr"] = $_POST["cpn_taxnr"];
    $cpnInfo["cpn_phone"] = $_POST["cpn_phone"];
    $cpnInfo["cpn_fax"] = $_POST["cpn_fax"];
    $cpnInfo["cpn_url"] = $_POST["cpn_url"];
    $cpnInfo["cpn_email"] = $_POST["cpn_email"];
    $cpnInfo["cpn_defaultpayterm"] = $_POST["cpn_defaultpayterm"];
    $cpnInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];

    $result = phpaga_companies_validdata($cpnInfo);

    if (PhPagaError::isError($result))
        $status = $result->getFormattedMessage();
    else
    {
        $new_cpn_id = phpaga_companies_add($cpnInfo);

        if (PhPagaError::isError($new_cpn_id))
            $status = $new_cpn_id->getFormattedMessage();
        else {
            header("Location: company.php?id=".$new_cpn_id);
            exit;
        }
    }
}
else
{
    $cpnInfo["cpn_name"] = "";
    $cpnInfo["cpn_address"] = "";
    $cpnInfo["cpn_address2"] = "";
    $cpnInfo["cpn_city"] = "";
    $cpnInfo["cpn_region"] = "";
    $cpnInfo["cpn_zip"] = "";
    $cpnInfo["ctr_id"] = PHPAGA_DEFAULTCOUNTRY;
    $cpnInfo["ccat_id"] = "";
    $cpnInfo["cpn_taxnr"] = "";
    $cpnInfo["cpn_phone"] = "";
    $cpnInfo["cpn_fax"] = "";
    $cpnInfo["cpn_url"] = "";
    $cpnInfo["cpn_email"] = "";
    $cpnInfo["cpn_defaultpayterm"] = "";
}

$cpnInfo["cpn_id"] = null;

phpaga_header(array("menuitem" => "core"));

$select_ccat = phpaga_companies_getCategories();
$select_ctr = phpaga_countries_getCountryList();

$tpl = new psmarty;

$tpl->assign("cpnInfo", $cpnInfo);

$tpl->assign("select_ctr", $select_ctr);
$tpl->assign("select_ccat", $select_ccat);
$tpl->assign("STATUSMSG", $status);
$tpl->assign("FORM_ACTION",
             basename($_SERVER["PHP_SELF"]));

$tpl->display("edit_company.tpl.html");

phpaga_footer();

?>
