<?php
  /**************************************************************************\
  * phpGroupWare - Inventory                                                 *
  * (http://www.phpgroupware.org)                                            *
  * Written by Bettina Gille  [ceb@phpgroupware.org]                         *
  * --------------------------------------------------------                 *
  *  This program 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.                                              *
  \**************************************************************************/
/* $Id: delivery_print.php,v 1.20.2.1 2001/06/22 02:51:10 bettina Exp $ */

    $phpgw_info["flags"] = array("currentapp" => "inv",
				   "noheader" => True,
				   "nonavbar" => True);

    include("../header.inc.php");

    $t = new Template(PHPGW_APP_TPL);
    $t->set_file(array( "delivery_print_t" => "delivery_print.tpl",
                      "delivery_print"   => "delivery_print.tpl"));

    $t->set_block("delivery_print_t", "delivery_print", "print");

    if ($phpgw_info["server"]["db_type"]=="pgsql") { $join = " JOIN "; }
    else { $join = " LEFT JOIN "; }

    $d = CreateObject('phpgwapi.contacts');

    if (isset($phpgw_info["user"]["preferences"]["inv"]["abid"])) {
    $t->set_var("error","");

    $myaddress = $phpgw_info["user"]["preferences"]["inv"]["abid"];

    $cols = array('n_given' => 'n_given',
                'n_family' => 'n_family',
                'org_name' => 'org_name',
                'org_unit' => 'org_unit',
                'adr_one_street' => 'adr_one_street',
                'adr_one_locality' => 'adr_one_locality',
                'adr_one_postalcode' => 'adr_one_postalcode',
                'adr_one_region' => 'adr_one_region',
                'adr_one_countryname' => 'adr_one_countryname');

    $address = $d->read_single_entry($myaddress,$cols);

    $t->set_var('ad_company',$address[0]['org_name']);
    $t->set_var('ad_department',$address[0]['org_unit']);
    $t->set_var('ad_firstname',$address[0]['n_given']);
    $t->set_var('ad_lastname',$address[0]['adr_one_street']);
    $t->set_var('ad_street',$address[0]['adr_one_postalcode']);
    $t->set_var('ad_zip',$address[0]['adr_one_locality']);
    $t->set_var('ad_city',$address[0]['adr_one_locality']);
    $t->set_var('ad_state',$address[0]['adr_one_region']);
    $t->set_var('ad_country',$address[0]['adr_one_countryname']);
    }
    else { $t->set_var('error',lang('Please select your address in preferences !')); }

    $t->set_var("site_title",$phpgw_info["site_descr"]);
    $charset = $phpgw->translation->translate("charset");
    $t->set_var("charset",$charset);
    $t->set_var("font",$phpgw_info["theme"]["font"]);
    $t->set_var("to","");
    $t->set_var(lang_delivery,lang("Delivery ID"));
    $t->set_var(lang_order_descr,lang("Order"));
    $t->set_var(lang_pos,lang("Pos."));
    $t->set_var(lang_date,lang("Delivery date"));
    $t->set_var(lang_order_descr,lang("Order description"));
    $t->set_var(lang_piece,lang("Piece"));
    $t->set_var(lang_product_id,lang("Product ID"));
    $t->set_var(lang_product_name,lang("Name"));
    $t->set_var(lang_serial,lang("Serial number"));

    if (!$delivery_id) {
	$phpgw->db->query("SELECT phpgw_inv_orders.customer,phpgw_inv_orders.descr,phpgw_inv_delivery.num,phpgw_inv_delivery.order_id, "
			. "phpgw_inv_delivery.date FROM phpgw_inv_delivery,phpgw_inv_orders WHERE "
			. "phpgw_inv_delivery.order_id='$order_id' AND phpgw_inv_orders.id='$order_id'");
    }
    else {
	$phpgw->db->query("SELECT phpgw_inv_orders.customer,phpgw_inv_orders.descr,phpgw_inv_delivery.id,phpgw_inv_delivery.num, "
                 . "phpgw_inv_delivery.order_id,phpgw_inv_delivery.date FROM phpgw_inv_delivery,phpgw_inv_orders WHERE "
                 . "phpgw_inv_delivery.id='$delivery_id' AND phpgw_inv_delivery.order_id=phpgw_inv_orders.id");
    }

    if ($phpgw->db->next_record()) {
    $custadr= $phpgw->db->f("customer");
                                                                                                                                                                             
    $cols = array('n_given' => 'n_given',
                 'n_family' => 'n_family',
                 'org_name' => 'org_name',
                 'org_unit' => 'org_unit',
               'adr_one_street' => 'adr_one_street',
             'adr_one_locality' => 'adr_one_locality',
           'adr_one_postalcode' => 'adr_one_postalcode',
               'adr_one_region' => 'adr_one_region',
          'adr_one_countryname' => 'adr_one_countryname',
                    'title' => 'title');

    $customer = $d->read_single_entry($custadr,$cols);

    $t->set_var('title',$customer[0]['title']);
    $t->set_var('firstname',$customer[0]['n_given']);
    $t->set_var('lastname',$customer[0]['n_family']);
    $t->set_var('company',$customer[0]['org_name']);
    $t->set_var('department',$customer[0]['org_unit']);
    $t->set_var('street',$customer[0]['adr_one_street']);
    $t->set_var('zip',$customer[0]['adr_one_postalcode']);
    $t->set_var('city',$customer[0]['adr_one_locality']);
    $t->set_var('state',$customer[0]['adr_one_region']);
    $t->set_var('country',$customer[0]['adr_one_countryname']);

    $t->set_var("delivery_day",date("j",$phpgw->db->f("date")));
    $t->set_var("delivery_month",date("n",$phpgw->db->f("date")));
    $t->set_var("delivery_year",date("Y",$phpgw->db->f("date")));
    $t->set_var("delivery_num",$phpgw->strip_html($phpgw->db->f("num")));
    $order_descr = $phpgw->strip_html($phpgw->db->f("descr"));
    if (!$order_descr) $order_descr = "&nbsp;";
    $t->set_var("order_descr",$order_descr);
    }

    $pos = 0;

    if (!$delivery_id) {
    $phpgw->db->query("SELECT phpgw_inv_products.*,phpgw_inv_orderpos.piece "
                . "FROM phpgw_inv_products $join phpgw_inv_orderpos ON phpgw_inv_products.con=phpgw_inv_orderpos.product_id "                                                                                         
                . "WHERE phpgw_inv_orderpos.order_id='$order_id'");
    }
    else {
    $phpgw->db->query("SELECT phpgw_inv_products.*,phpgw_inv_orderpos.order_id,phpgw_inv_orderpos.piece FROM phpgw_inv_products "
		    . "$join phpgw_inv_orderpos ON phpgw_inv_products.con=phpgw_inv_orderpos.product_id $join phpgw_inv_deliverypos ON "
		    . "phpgw_inv_deliverypos.product_id=phpgw_inv_products.con $join phpgw_inv_delivery ON "
		    . "phpgw_inv_delivery.order_id=phpgw_inv_orderpos.order_id "
		    . "WHERE phpgw_inv_delivery.id='$delivery_id' AND phpgw_inv_deliverypos.delivery_id='$delivery_id'");
    }
    while ($phpgw->db->next_record()) {
	$pos++;
	$t->set_var("pos",$pos);
	$t->set_var("piece",$phpgw->db->f("piece"));
	$t->set_var("id",$phpgw->db->f("id"));
        $name = $phpgw->strip_html($phpgw->db->f("name"));                                                                                                         
        if (!$name) $name = "&nbsp;";
        $t->set_var("name",$name);
        $serial = $phpgw->strip_html($phpgw->db->f("serial"));
        if (!$serial) $serial = "&nbsp;";
	$t->set_var("serial",$serial);
        $t->parse("print", "delivery_print", true);
    }

    $t->parse("out", "delivery_print_t", true);
    $t->p("out");

?>
