<?php
/**
 * phpaga
 *
 * edit company
 *
 * Edit a company's data
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: edit_company.php 726 2006-08-05 22:34:30Z florian $
 *
 * Copyright (c) 2002, 2003, 2004, 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");

$result   = PHPAGA_ERR_NONE;
$s_result = PHPAGA_ERR_NONE;
$status   = "";

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

    phpaga_companies_isowner($isowner, $_POST["cpn_id"], $_SESSION["auth_user"]["pe_id"]);

    if (!$isowner && !phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERCOMPANIES))
    {
        phpaga_header(array("menuitem" => "core"));
        phpaga_message(_("No access"), _("You are not allowed to modify this record."));
        phpaga_footer();
        exit;
    }

    $cpnInfo["cpn_id"] = $_POST["cpn_id"];
    $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"];

    $result = phpaga_companies_validdata($cpnInfo);

    if (phpaga_errorhappened($result))
        $status = phpaga_returnerror($result);
    else
    {
        $update_result = phpaga_companies_update($cpnInfo);

        if (phpaga_errorhappened($update_result))
            $status = phpaga_returnerror($phpaga_error[$update_result]);
        else
            header("Location: company.php?id=".$cpnInfo["cpn_id"]);
    }
}
else
{

    if (isset($_GET["id"]))
        $cpn_id = $_GET["id"];
    else
        $cpn_id = "";

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

    $s_result = phpaga_companies_getInfo($cpn_id, $cpnInfo);

    if (phpaga_errorhappened($s_result))
        $status = phpaga_returnerror($s_result);

}

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

if ($s_result != PHPAGA_ERR_NONE)
{
    if ($s_result == PHPAGA_ERR_DB_NO_ROWS)
        phpaga_error(_("The company you are looking for could not be found."));
    else
        phpaga_error($s_result);
}
else if (($_SESSION["auth_user"]["pe_id"] != $cpnInfo["pe_id"])
         && !phpaga_user_hasperm(PHPAGA_PERM_MANAGE_OTHERCOMPANIES))
    phpaga_message(_("No access"), _("You are not allowed to modify this record."));
else
{

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

    $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 ();

?>