<?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)
 *
 */

  /*===================================================
    ShowFormCreateInvoice()
  ===================================================*/

  function ShowFormCreateInvoice() {

    // vars global configuration
    global $admin_template_path;

    // vars url & form

    // vars messages

    // vars template

    DisplayTemplate($admin_template_path . "inv_create_form.html");
  }


  /*===================================================
    ProcessFormCreateInvoice()
  ===================================================*/

  function ProcessFormCreateInvoice() {

    // vars global configuration
    global $dbConn, $admin_template_path, $site_url, $theme_path, $active_theme;

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

    // vars messages
    global $msg;

    // vars template
    global $message, $back;

    // 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)) {

      // generate invoice_id
      $exist = true;
      while ($exist) {
        mt_srand((double)microtime()*1000000);
        $invoice_id = mt_rand();
        $invoice_id = substr($invoice_id, -5, 5);

        $query = "select invoice_id from idx_invoice where invoice_id = $invoice_id";
        $result = $dbConn->Execute($query);
        if($result->Fields('invoice_id')) {
          $exist = true;
        }
        else {
          $exist = false;
        }
      }

      // create invoice
      $query = "insert into idx_invoice 
               (invoice_id, invoice_date, due_date, invoiced_to, email, service, description, total, comment, paid) values 
               ('$invoice_id', now(), '$due_date', '$invoiced_to', '$email', '$service', '$description', '$total', '$comment', 0)";
      $result = $dbConn->Execute($query);
      $message = $msg['20484'];
      $back = "<a href=inv_create.php>Back</a>";

      if ($sendnow) {
        // get config
        $query = "select * from idx_invoice_config";
        $result = $dbConn->Execute($query);
        $company = $result->Fields('company');
        $company_email = $result->Fields('email');

        // send email
        $body = EvalTemplate($theme_path . $active_theme . "/mail/inv_ask_payment.mail", "\$invoice_id,\$site_url");
        $subject = "$company, Invoice #$invoice_id";
        @mail($email,$subject, $body, "From: $company_email\nX-Mailer: INDEXU_X-Mailer/1.0");
      }

    }
    else $message = $error_msg;

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


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

  include "../application.php";

  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormCreateInvoice();
  }
  elseif ($pflag == 'create') {
    ProcessFormCreateInvoice();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>