<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php

require_once 'html.php';

class Customer extends HTML {

  function Customer () {
    global $dbi;
    $this->db = &$dbi;
  }

  function Add ($in_ary) {
    $add = $this->prep_add_array ($in_ary);

    if (!$add[CUSTOMER_ID]) {
      $add[CUSTOMER_ID] = $this->db->nextId ('customer_customer_id');
    }

    $this->Set_Updated_by ($add);

    $h = $this->db->prepare ('
      insert into customer (
        customer_id,
        customer_name,
        updated_by
      ) values (
        ?, ?, ?
      )
    ');
    $res = $this->db->execute($h, array(
      $add[CUSTOMER_ID],
      $add[CUSTOMER_NAME],
      $add[UPDATED_BY]
    ));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    return $add[CUSTOMER_ID];
  }

  function List_All ($order='') {

    if (!$order) { $order = 'customer_name'; }

    $sql = "
       select customer_id,
              customer_name
         from customer
     order by $order
    ";
    return $this->db->getAll($sql, array());
  }

  function Set_Customer_To ($cid) {
    $h = $this->db->prepare ('
         select customer_id,
                customer_name
           from customer
          where customer_id = ?
    ');
    $res = $this->db->execute( $h, array($cid) );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }

    $r = $res->fetchRow(DB_FETCHMODE_ASSOC);

    foreach ($r as $k => $v) {
      $k = strtolower ($k);
      $this->$k = $v;
    }
  }

  function Update () {
    $this->Set_Audit_Info ($this);

    $h = $this->db->prepare ('
             update customer
                set customer_name = ?
              where customer_id = ?
    ');
    $res = $this->db->execute($h, array(
      $this->customer_name,
      $this->customer_id
    ));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return 0;
    }
    return 1;
  }
}
?>
