<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  /*===================================================
    ShowFormEditInvoice()
  ===================================================*/

  function ShowFormEditInvoice() {

    // vars global configuration
    global $dbConn, $admin_template_path;

    // vars url & form
    global $due_date,$invoiced_to,$service,$description,$email,$total,$comment,$id;

    // vars messages

    // vars template

    $query = "select * from idx_invoice where invoice_id = '$id'";
    $result = $dbConn->Execute($query);

    $due_date = $result->Fields('due_date');
    $invoiced_to = $result->Fields('invoiced_to');
    $service = $result->Fields('service');
    $description = $result->Fields('description');
    $email = $result->Fields('email');
    $total = $result->Fields('total');
    $comment = $result->Fields('comment');

    DisplayTemplate($admin_template_path . "inv_edit_form.html",
      "\$due_date,\$invoiced_to,\$service,\$description,\$email,\$total,\$comment,\$id");
  }


  /*===================================================
    ProcessFormEditInvoice()
  ===================================================*/

  function ProcessFormEditInvoice() {

    // vars global configuration
    global $dbConn, $admin_template_path;

    // vars url & form
    global $due_date,$invoiced_to,$service,$description,$email,$total,$comment,$id;

    // vars messages
    global $msg;

    // vars template
    global $message;

    // verify input

    if(empty($due_date)) $error_msg = $msg['20461'];
    elseif(empty($invoiced_to)) $error_msg = $msg['20462'];
    elseif(empty($email)) $error_msg = $msg["20154"];
    elseif(!IsEmailAddress($email)) $error_msg = $msg["20155"];
    elseif(empty($service)) $error_msg = $msg['20463'];
    elseif(empty($description)) $error_msg = $msg['20464'];
    elseif(empty($total)) $error_msg = $msg['20465'];

    if(empty($error_msg)) {
      $query = "update idx_invoice set
        due_date = '$due_date',
        invoiced_to = '$invoiced_to',
        email = '$email',
        service = '$service',
        description = '$description',
        total = '$total',
        comment = '$comment'
        where invoice_id = '$id'
      ";
      $result = $dbConn->Execute($query);
      $message = $msg['20481'];
    }
    else $message = $error_msg;

    DisplayTemplate($admin_template_path . "message.html", "\$message");
  }


  /*===================================================
    main
  ===================================================*/

  include "../application.php";
  
  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormEditInvoice();
  }
  elseif ($pflag == 'edit') {
    ProcessFormEditInvoice();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>