<?php
/**
 * phpaga
 *
 * edit company
 *
 * Edit a company's data
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: edit_company.php 857 2007-09-19 20:12: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 = null;
$status = null;

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

    $isowner = phpaga_companies_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 (PhPagaError::isError($result))
        $status = $result->getFormattedMessage();
    else
    {
        $update_result = phpaga_companies_update($cpnInfo);

        if (PhPagaError::isError($update_result))
            $status = $update_result->getFormattedMessage();
        else {
            header('Location: company.php?id='.$cpnInfo['cpn_id']);
            exit;
        }
    }
}
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'] = '';

    $cpnInfo = phpaga_companies_getInfo($cpn_id);

    if (PhPagaError::isError($cpnInfo))
        $status = $cpnInfo->getFormattedMessage();

}

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

if (isset($_GET['id']) && PhPagaError::isError($cpnInfo))
    phpaga_error(_('The company you are looking for could not be found.'));
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 {

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

?>