<?
/* index.php: $pubmodule, $pubtask, $pubitemid */
/*
$pubsubjectid = $_REQUEST["subjectid"];
$pubtypeid = $_REQUEST["typeid"];
$pubgroupid = $_REQUEST["groupid"];
*/
$pubprojectid = $_REQUEST["pid"];
$pubtype = $_REQUEST["type"];

// filter forms..
/*
$filtersessionname=$_POST['sessionname'];
$filteruserpkey=$_POST['userpkey'];
$filterstartdate=$_POST['startdate'];
$filterstopdate=$_POST['stopdate'];
$realstartdate=converttounix($filterstartdate);
$realstopdate=converttounix($filterstopdate)+86399;
*/

$pubpageno1 = $_REQUEST['pageno1'];
if ($pubpageno1=="") $pubpageno1=1;

?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function invoice_button_add(projectpkey) {
  document.location='index.php?module=invoice&task=add&pid='+projectpkey;
}

function invoice_button_edit(invoicepkey) {
  document.location='index.php?module=invoice&task=edit&itemid='+invoicepkey;
}

function invoice_button_view(invoicepkey) {
  document.location='index.php?module=invoice&task=view&itemid='+invoicepkey;
}

function invoice_button_delete(invoicepkey) {
  document.location='index.php?module=invoice&task=delete&itemid='+invoicepkey;
}

function invoice_button_save() {
  if (validateForm()==true) {
    document.invoiceform.job.value='invoice_add'; document.invoiceform.submit();
  }
}

function invoice_button_updateinvoices() {
  if (validateForm()==true) {
    if (submitForm()==true) {
      document.invoiceform.job.value='invoice_add'; document.invoiceform.submit();
    }
  }
}

function invoice_button_update() {
  if (validateForm()==true) {
    document.invoiceform.job.value='invoice_edit'; document.invoiceform.submit();
  }
}

function invoice_button_purge() {
  document.invoiceform.job.value='invoice_remove'; document.invoiceform.submit();
}

function invoice_button_backtolist() {
  document.location='index.php?module=invoice&task=main';
}

function invoice_button_back() {
  history.go(-1);
}

</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/invoiceModel.php');
$myinvoiceModel=new invoiceModel;

?>

<?

// ................................................................................
// data retrieval functions..
// ................................................................................


// ................................................................................
// data modifications functions..
// ................................................................................


// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="Invoice";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="List";
} else if ($pubtask=="list") {
  $subtitle="Invoice List";
} else if ($pubtask=="add") {
  $subtitle="Add Invoice";
} else if ($pubtask=="edit") {
  $subtitle="Edit Invoice";
} else if ($pubtask=="view") {
  $subtitle="View Invoice";
} else if ($pubtask=="delete") {
  $subtitle="Delete Invoice";
} else if ($pubtask=="additem") {
  $subtitle="Add Invoice Item";
} else if ($pubtask=="edititem") {
  $subtitle="Edit Invoice Item";
} else if ($pubtask=="viewitem") {
  $subtitle="View Invoice Item";
} else if ($pubtask=="deleteitem") {
  $subtitle="Delete Invoice Item";

} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." ··» ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="submenu")) {

  include_once('models/poModel.php');
  $mypoModel=new poModel;

  //$invoice_list = $myinvoiceModel->getlist_invoicebyproject($pubprojectpkey);
  $po_list = $mypoModel->getlist_pobyproject($pubprojectpkey, "3", "3");

  //include_once('views/invoice-list.tpl');
  //include_once('views/general-invalidtask.php');

  //echo $_SERVER['QUERY_STRING'];
  include_once('views/invoice-list.php');   		



} else if ($pubtask=="list") {

  $invoice_list = $myinvoiceModel->getlist_invoicebyproject($pubprojectpkey);
  include_once('views/invoice-list.tpl');

} else if (($pubtask=="add")||($pubtask=="edit")) {

  /*
  $mro_data = $mymroModel->getdata_mro($pubitemid);
  if ($mro_data['mro_type']=="") $mro_data['mro_type']=$pubtype;
  if ($mro_data['mro_projectpkey']=="") $mro_data['mro_projectpkey']=$pubprojectpkey;
  include_once('views/mro-edit.tpl');
  */


 

  include_once('models/supplierModel.php');
  include_once('models/mroModel.php');
  include_once('models/itqModel.php');
  include_once('models/poModel.php');
  include_once('models/doModel.php');
  $mysupplierModel=new supplierModel;
  $mymroModel=new mroModel;
  $myitqModel=new itqModel;
  $mypoModel=new poModel;
  $mydoModel=new doModel;

  $po_data = $mypoModel->getdata_po($pubitemid);
  $po_items = $mypoModel->getlist_poitems($pubitemid);
  $poamount = $mypoModel->getamount_poitems($pubitemid);

  $supplier_data = $mysupplierModel->getdata_supplier($po_data['po_supplierpkey']);
  $mro_data = $mymroModel->getdata_mro($po_data['po_mropkey']);
  $itq_data = $myitqModel->getdata_itq($po_data['po_itqpkey']);

  include_once('views/invoice-add.tpl');



} else if ($pubtask=="view") {

  $mro_data = $mymroModel->getdata_mro($pubitemid);
  $item_list = $mymroModel->getlist_mroitems($pubitemid);
  include_once('views/mro-view.tpl');

} else if ($pubtask=="delete") {

  $mro_data = $mymroModel->getdata_mro($pubitemid);
  $item_list = $mymroModel->getlist_mroitems($pubitemid);
  include_once('views/mro-view.tpl');

} else if (($pubtask=="additem")||($pubtask=="edititem")) {

  $item_data = $mymroModel->getdata_mroitem($pubitemid);
  include_once('views/mro-edititem.tpl');

} else if ($pubtask=="viewitem") {

  $item_data = $mymroModel->getdata_mroitem($pubitemid);
  include_once('views/mro-viewitem.tpl');

} else if ($pubtask=="deleteitem") {

  $item_data = $mymroModel->getdata_mroitem($pubitemid);
  include_once('views/mro-viewitem.tpl');

} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="submenu")) {

  echo "<br><hr size=1>";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"invoice_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="list") {

  echo "<br><hr size=1>";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"invoice_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="add") {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_invoice_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"update\" id=\"update\" onClick=\"invoice_button_updateinvoices();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Changes</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_backtolist();\"><img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back to Invoice List</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="edit") {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"invoice_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Invoice</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"invoice_button_edit('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"invoice_button_delete('".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"invoice_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="delete") {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS INVOICE?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"invoice_button_purge();\">&nbsp;Yes, Delete Invoice&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

} else if ($pubtask=="additem") {

  echo "<br><hr size=1>";
  echo "<button name=\"save\" id=\"save\" onClick=\"invoice_button_saveitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Invoice Item</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="edititem") {

  echo "<br><hr size=1>";
  echo "<button name=\"update\" id=\"update\" onClick=\"invoice_button_updateitem();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Invoice Item</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="viewitem") {

  echo "<br><hr size=1>";
  echo "<button name=\"edit\" id=\"edit\" onClick=\"invoice_button_edititem('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"invoice_button_deleteitem('".$pubitemid."');\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"invoice_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="deleteitem") {

  echo "<br><hr size=1>";
  echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS INVOICE ITEM?</b></font><br><br>";
  echo "<button name=\"delete\" id=\"delete\" onClick=\"invoice_button_purgeitem();\">&nbsp;Yes, Delete Invoice Item&nbsp;</button>&nbsp;";
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"invoice_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";

}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>