<?php
    class TRA_CARGTICKET extends TRA
    {
        public function tra_cargticket_javainit()
        {

        }

        public function fn_cargticket_init()
        {
            if(isset($_REQUEST['task'])){$task=trim(strtolower($_REQUEST['task']));}
            if(isset($_POST['task'])){$task=trim(strtolower($_POST['task']));}
            if($_REQUEST||$_POST){if($_REQUEST['sec']||$_POST['sec']){$task=$this->dec($task);}}

            switch($task)
            {
                case "default":
                    $this->fn_default_cargticket();
                break;

                case $task:
                    $task="fn_".$task;
                    if(method_exists($this,$task)){$this->$task();}
                break;

                default:
                    $this->fn_default_cargticket();
                break;
            }
        }
        public function fn_default_cargticket()
        {
            echo $this->FN_CREATEHEADER();
            echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />";
            echo "<style>
                      .besidecboidcard {margin-left:280px;margin-top:-5px;position:absolute;}
                      .besidetxttiketnumber {margin-left:325px;margin-top:-6px;position:absolute;}
                      .besidelabel {margin-left:280px;margin-top:-5px;position:absolute;}                      
                      .containerpicture {margin-left:0px;margin-top:0px;}
                      .containerbtnlookcategory {margin-left:400px;margin-top:-19px;position:absolute;}
                      .besidelabelprice {margin-left:275px;margin-top:-6px;position:absolute;}                      
                      .besidecbofrom {margin-left:305px;margin-top:-5px;position:absolute;}                      
                      .besidebtnsave {margin-left:104px;margin-top:-6px;position:absolute;}
                      .besidebtnupdate {margin-left:198px;margin-top:-6px;position:absolute;}
                      .besidebtnsubmit {margin-left:292px;margin-top:-6px;position:absolute;}
                      .besidebtndelete {margin-left:288px;margin-top:-6px;position:absolute;}
                  </style>";
            echo "<script>
                       var myForm,myFormsearch,myFormcat,dgcargticket,dwins,dhxwin,dgfindcategory;
                       function initObject()
                       {";
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"block","width"=>"500",
                                                     "list"=>array(
                                                                      array("type"=>"hidden","name"=>"txt_id"),
                                                                      array("type"=>"hidden","name"=>"txt_idcategory"),
                                                                      array("type"=>"hidden","name"=>"txt_modifcount"),
                                                                      array("type"=>"hidden","name"=>"txt_units"),
                                                                      array("type"=>"combo","name"=>"cbo_idship","label"=>"Ship Name","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"0","placeholder"=>"Please enter your email",
                                                                            "tooltip"=>"Pilih Ship Name First. Obrigado","validate"=>"NotEmpty","maxLength"=>"11","className"=>"txtcenter"),
                                                                      array("type"=>"input","name"=>"txt_tiketnumber","label"=>"Ticket Number","info"=>"true","inputWidth"=>"135","value"=>"",
                                                                            "tooltip"=>"Entry Ticket Number First. Obrigado","maxLength"=>"40","className"=>"txtcenter","readonly"=>"true"),
                                                                      array("type"=>"input","name"=>"txt_tiketnumberpassenger","label"=>"","info"=>"true","inputWidth"=>"135","value"=>"-","note"=>array("text"=>"Passenger Tiket ( Optional )"),
                                                                            "tooltip"=>"Entry Ticket Number First. Obrigado","maxLength"=>"40","className"=>"txtleft besidetxttiketnumber"),
                                                                      array("type"=>"input","name"=>"txt_voynumber","label"=>"Voyage Number","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"","offsetTop"=>"20",
                                                                            "tooltip"=>"Entry Voyage Number First. Obrigado","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
//                                                                      array("type"=>"input","name"=>"txt_inctiket","label"=>"No. Sequence","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
//                                                                            "tooltip"=>"Entry No. Sequence First. Obrigado","validate"=>"NotEmpty","maxLength"=>"11","className"=>"txtcenter"),
                                                                      array("type"=>"input","name"=>"txt_passengername","label"=>"Passenger Name","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Entry Passenger Name First. Obrigado","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
                                                                      array("type"=>"combo","name"=>"cbo_idcardtype","label"=>"ID Card","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Pilih ID Card First. Obrigado","validate"=>"NotEmpty","maxLength"=>"11","className"=>"txtcenter"),
                                                                      array("type"=>"newColumn"),
                                                                      array("type"=>"input","name"=>"txt_cardnumber","label"=>"","required"=>"true","info"=>"true","inputWidth"=>"120","value"=>"",
                                                                            "tooltip"=>"Entry Picture First. Obrigado","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft besidecboidcard",
                                                                            "note"=>array("text"=>"Insert ID card Number")),
                                                                      //array("type"=>"container","name"=>"containerpicture","label"=>"Take Picture","className"=>"containerpicture","offsetTop"=>"20"),
                                                                      
                                                                      array("type"=>"hidden","name"=>"txt_picdata","label"=>"Picture","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"aportildefault.png",
                                                                            "tooltip"=>"Entry Picture First. Obrigado","validate"=>"NotEmpty","maxLength"=>"100","className"=>"txtleft"),
                                                                      
                                                                      array("type"=>"calendar","name"=>"txt_dates","label"=>"Dates","required"=>"true","info"=>"true","inputWidth"=>"90","readonly"=>"true","value"=>date("Y-m-d"),
                                                                            "tooltip"=>"Entry Dates First. Obrigado","validate"=>"NotEmpty","maxLength"=>"","className"=>"txtcenter","dateFormat"=>"%Y-%m-%d"),
                                                                      
                                                                      array("type"=>"combo","name"=>"cbo_idroutestart","label"=>"From","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Pilih From First. Obrigado","validate"=>"NotEmpty","maxLength"=>"6","className"=>"txtcenter"),
                                                                      array("type"=>"newColumn"),
                                                                      
                                                                      array("type"=>"label","name"=>"lblmeter","label"=>"to","className"=>"besidelabel"),
                                                                      array("type"=>"combo","name"=>"cbo_idrouteend","label"=>"","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Pilih To First. Obrigado","validate"=>"NotEmpty","maxLength"=>"6","className"=>"txtcenter besidecbofrom"),
                                                                      array("type"=>"newColumn"),                                                                      
                                                                      array("type"=>"container","name"=>"containerlookcategory","label"=>"","className"=>"containerbtnlookcategory","offsetTop"=>"20"),
                                                                      
                                                                      array("type"=>"input","name"=>"txt_cargodescription","label"=>"Category","required"=>"true","info"=>"true","inputWidth"=>"280","value"=>"",
                                                                            "tooltip"=>"Insert Category First. Thank you","validate"=>"NotEmpty","maxLength"=>"11","className"=>"txtleft","readonly"=>true),
                                                                      
                                                                      array("type"=>"input","name"=>"txt_quantity","label"=>"Quantity","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Entry Quantity First. Obrigado","validate"=>"NotEmpty,ValidNumeric","maxLength"=>"","className"=>"txtright"),                                                                      
                                                                      array("type"=>"newColumn"),
                                                                      array("type"=>"label","name"=>"lblmeasure","label"=>"Measure","className"=>"besidelabelprice"),

                                                                      array("type"=>"input","name"=>"txt_weight","label"=>"Weight","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Entry Weight First. Obrigado","validate"=>"NotEmpty,ValidNumeric","maxLength"=>"","className"=>"txtright"),                                                                      
                                                                      
                                                                      array("type"=>"input","name"=>"txt_price","label"=>"Price","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0","readonly"=>true,
                                                                            "tooltip"=>"Entry Price First. Obrigado","validate"=>"NotEmpty","maxLength"=>"","className"=>"txtright","readonly"=>"true"),
                                                                      array("type"=>"newColumn"),
                                                                      array("type"=>"label","name"=>"lblmeter","label"=>"\$","className"=>"besidelabelprice"),
                                                                      
                                                                      array("type"=>"input","name"=>"txt_totalprice","label"=>"Total","required"=>"true","info"=>"true","inputWidth"=>"90","value"=>"0",
                                                                            "tooltip"=>"Entry Total First. Obrigado","validate"=>"NotEmpty","maxLength"=>"","className"=>"txtright","readonly"=>"true"),
                                                                      array("type"=>"newColumn"),
                                                                      array("type"=>"label","name"=>"lblmeter","label"=>"\$","className"=>"besidelabelprice"),
                                                                      
                                                                      array("type"=>"button","name"=>"btnsave","value"=>"Save","className"=>"button_update","offsetLeft"=>"20","offsetTop"=>"15"),
                                                                      array("type"=>"button","name"=>"btnupdate","value"=>"Update","className"=>"button_saveas besidebtnsave","offsetLeft"=>"10","disabled"=>"1"),
                                                                      //array("type"=>"button","name"=>"btnsubmit","value"=>"Submit","className"=>"button_submit besidebtnupdate","offsetLeft"=>"0","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btndelete","value"=>"Delete","className"=>"button_delete besidebtnupdate","offsetLeft"=>"20","disabled"=>"1"),
                                                                      array("type"=>"button","name"=>"btncancel","value"=>"Cancel","className"=>"button_cancel besidebtndelete","offsetLeft"=>"30","disabled"=>"0")
                                                     )
                                              )
                                       );

                            $this->FN_DHTMLXFORM("myForm","divcargticketnew",$formdata);
                            $mycontainerpic="var divpicture=document.createElement('DIV');";
                            $mycontainerpic.="divpicture.id='divpicture';divpicture.style.height='100%';";
                            $mycontainerpic.="var divpicturecontent='<table id=\"tbfindinvoice\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"margin-top:0px;margin-left:-5px;\">';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'<tbody>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'    <tr>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'        <td>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'            <div id=\"divimage\" style=\"height:180px;width:280px;background-color:#2c3e50;margin-left:5px;\">';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'                <image id=\"imgtiket\" style=\"height:180px;width:280px;\" />';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'            </div>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'            <div style=\"position:absolute;z-index:100;margin-top:-50px;margin-left:10px\">';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'                <button id=\"btntake\" onClick=\"fn_showCamera()\"><i class=\"fa fa-camera\" style=\"font-size:30pt\"></i></button>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'            </div>';";                                                            
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'        </td>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'    </tr>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'</tbody>';";
                            $mycontainerpic.="divpicturecontent=divpicturecontent+'</table>';";

                            $mycontainerpic.="divpicture.innerHTML=divpicturecontent;";
                            $mycontainerpic.="myForm.getContainer('containerpicture').appendChild(divpicture);";

                            //echo $mycontainerpic;

                            $mycontainerbtncat="var divbtncat=document.createElement('DIV');";
                            $mycontainerbtncat.="divbtncat.id='divbtncat';divbtncat.style.height='100%';";
                            $mycontainerbtncat.="var divbtncatcontent='<button id=\"btntake\" onClick=\"fn_showcategory()\"><i class=\"fa fa-upload\" style=\"font-size:14pt\"></i></button>';";
                            $mycontainerbtncat.="divbtncat.innerHTML=divbtncatcontent;";
                            $mycontainerbtncat.="myForm.getContainer('containerlookcategory').appendChild(divbtncat);";

                            echo $mycontainerbtncat;
                            
                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_ship_information|id,shipname')";
                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","cbo_idship","tbl_ship_information","$urlcbo","");

//                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_cargoprice_information|id,cargodescription')";
//                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","txt_cargodescription","tbl_cargoprice_information","$urlcbo","");

                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_passenger_identifycardtype|id,cardtype')";
                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","cbo_idcardtype","tbl_passenger_identifycardtype","$urlcbo","");

                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_route_information|id,routename')";
                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","cbo_idroutestart","tbl_route_information","$urlcbo","");

                            $urlcbo="fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_route_information|id,routename')";
                            //$urlother="myForm.getCombo('txt_cargodescription').clearAll();myForm.getCombo('txt_cargodescription').setComboText('');";
                            //$urlother.="var url=fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("dhtmlxcbofilter")."&tableobj=tbl_cargoprice_information|id,cargocategory,cargodescription:cargodescription,id&and=idroutestart|'+myForm.getItemValue('cbo_idroutestart')+',idrouteend|'+val);";
                            //$urlother.="alert(url);";
                            //$urlother.=$this->FN_COMBOVALIDATIONVALUENEW("myForm","txt_cargodescription","tbl_cargoprice_information","url","",true);
                            //$urlother="fn_showcategory(myForm.getItemValue('cbo_idroutestart'),val);";
                            //$urlother="fn_showcategory();";
                            $this->FN_COMBOVALIDATIONVALUENEW("myForm","cbo_idrouteend","tbl_route_information","$urlcbo","");

                            
                            
                            $arrerror=array("cbo_idship"=>"Error Found : Please Insert <b>Ship Name</b> First.Obrigado",
                                            "txt_idcategory"=>"Error Found : Please Insert <b>Category</b> First.Obrigado",
                                            "txt_tiketnumber"=>"Error Found : Please Insert <b>Ticket Number</b> First.Obrigado",
                                            "txt_voynumber"=>"Error Found : Please Insert <b>Voyage Number</b> First.Obrigado",
                                            //"txt_cargodescription"=>"Error Found : Insert Category <b>Category</b> first.Obrigado",
                                            "txt_passengername"=>"Error Found : Please Insert <b>Passenger Name</b> First.Obrigado",
                                            //"txt_picdata"=>"Error Found : Please Take Camera your <b>Identity</b> First.Obrigado",
                                            "cbo_idcardtype"=>"Error Found : Please Insert <b>ID Card</b> First.Obrigado",
                                            "txt_cardnumber"=>"Error Found : Please Insert <b>Card Number</b> First.Obrigado",
                                            "txt_dates"=>"Error Found : Please Insert <b>Dates</b> First.Obrigado",
                                            "cbo_idroutestart"=>"Error Found : Please Insert <b>From</b> First.Obrigado",
                                            "cbo_idrouteend"=>"Error Found : Please Insert <b>To</b> First.Obrigado",
                                            "txt_price"=>"Error Found : Please Insert <b>Price</b> First.Obrigado",
                                            "txt_quantity"=>"Error Found : Please Insert <b>Quantity</b> First.Obrigado",
                                            "txt_weight"=>"Error Found : Please Insert <b>Weight</b> First.Obrigado",
                                            "txt_totalprice"=>"Error Found : Please Insert <b>Total</b> First.Obrigado"
                                      );
                            $this->FN_DHTMLXFORMVALIDATIONERROR("myForm",$arrerror);

                            $arrayControls=array("txt_id"=>"","txt_idcategory"=>"","txt_modifcount"=>"","cbo_idship"=>"","txt_tiketnumber"=>"",
                                                 "txt_tiketnumberpassenger"=>"-","txt_voynumber"=>"","txt_cargodescription"=>"","txt_passengername"=>"",
                                                 "txt_picdata"=>"aportildefault.png","cbo_idcardtype"=>"","txt_dates"=>date("Y-m-d"),"txt_cardnumber"=>"","cbo_idroutestart"=>"",
                                                 "cbo_idrouteend"=>"","txt_price"=>"0","txt_quantity"=>"0","txt_weight"=>"0","txt_totalprice"=>"0"
                                          );

                            // === Cbo Ship Change
                            $cboship="if(myForm.getItemValue('cbo_idship')!='')";
                            $cboship.="{";
                            $cboship.="   var sp=ajaxuridhtmlx('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cargticket")."&task=".$this->enc("getheaderinfo")."&idship='+myForm.getItemValue('cbo_idship')).split('|');";
                            $cboship.="   if(trim(sp[0])=='failed'){fn_dhtmlxmessage('error',sp[1]);".$this->FN_DHTMLXWINDOWSHIDEPROGRESS().";return false;}";
                            $cboship.="   var tiketnumber=sp[1];";
                            $cboship.="   myForm.setItemValue('txt_tiketnumber',tiketnumber);";
                            $cboship.="}";
                            $onkeypresscboshipname=$this->FN_DHTMLXWINDOWSHOWPROGESS($cboship);
                            $onkeypresscboshipname.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();

                            // === Cbo Route End Change
                            //$onchangecboidrouteend="if(myForm.getItemValue('cbo_idrouteend')!='')";
                            //$onchangecboidrouteend.="{";
                            //$onchangecboidrouteend.="   var sp=ajaxuridhtmlx('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cargticket")."&task=".$this->enc("getprice")."&idroutestart='+myForm.getItemValue('cbo_idroutestart')+'&idrouteend='+myForm.getItemValue('cbo_idrouteend')).split('|');";
                            //$onchangecboidrouteend.="   if(trim(sp[0])=='failed'){fn_dhtmlxmessage('error',sp[1]);".$this->FN_DHTMLXWINDOWSHIDEPROGRESS().";return false;}";
                            //$onchangecboidrouteend.="   myForm.setItemValue('txt_price',sp[2]);";
                            //$onchangecboidrouteend.="   myForm.setItemValue('txt_totalprice',sp[2]);";
                            //$onchangecboidrouteend.="}";
                            
                            // === Form Event
                            $onobjkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();if(myForm.isItemEnabled('btnupdate')){myForm.setItemFocus('btnupdate');dhtmlXclickButton('btnupdate');}else{myForm.setItemFocus('btnsave');}return false;}";
                            
                            $onobjtxtquantity="if(ev.keyCode==13){";
                            $onobjtxtquantity.="    ev.stopPropagation();ev.preventDefault();";
                            $onobjtxtquantity.="    var totalprice=myForm.getItemValue('txt_price')*myForm.getItemValue('txt_quantity');";
                            //$onobjtxtquantity.="    alert(val);";
                            $onobjtxtquantity.="    myForm.setItemValue('txt_totalprice',totalprice);";
                            $onobjtxtquantity.="    myForm.setItemFocus('txt_weight');";
                            $onobjtxtquantity.="}";

                            $ontxtcboidrouteend="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();fn_showcategory();";
                            //$onformsenddata.="document.getElementById('imgtiket').src='';";                                                                              
                            $onbuttonclickbtnsave="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                              title:'Confirmation',text:'Are you will be saved and data is correctly?',type:'confirm',
                                                              callback:function(result){
                                                                  if(result)
                                                                  {";
                                                                      $params="'obj=".$this->enc("tra_cargticket")."&task=".$this->enc("saverecord")."'";
                                                                      $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_tra")."',$params);
                                                                      myForm.send(url,\"get\",function(loader, response){";
                                                                          $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                          $onformsenddata.="var sp=trim(response).split('|');
                                                                          //alert(response);
                                                                          if(sp[0]=='ok')
                                                                          {
                                                                              fn_dhtmlxmessage('','Ticket Number <b>'+myForm.getItemValue('txt_tiketnumber')+'</b> has been saved');";
                                                                              $onformsenddata.="myForm.setItemLabel('lblmeasure','Measure');";
                                                                              $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"cbo_idship");
                                                                              $onformsenddata.="fn_grid_loaddata();
                                                                          }
                                                                          else if(sp[0]=='failed')
                                                                          {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                          else
                                                                          {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                      });";
                                                                      $onbuttonclickbtnsave.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnsave.="              }
                                                              }
                                                        });
                                                    }";

                            //$onformsenddata.="document.getElementById('imgtiket').src='';";                                                                           
                            $onbuttonclickbtnupdt="if(myForm.validate())
                                                   {
                                                        dhtmlx.confirm({
                                                             title:'Confirmation',text:'Are you will be updated and data is correctly?',type:'confirm',
                                                             callback:function(result){
                                                                if(result)
                                                                {";
                                                                   $params="'obj=".$this->enc("tra_cargticket")."&task=".$this->enc("updrecord")."'";
                                                                   $onformsenddata="url=fn_dhtmlxsenddata('".$this->enc("obj_tra")."',$params);
                                                                   myForm.send(url,\"get\",function(loader, response){";
                                                                      $onformsenddata.=$this->FN_DHTMLXWINDOWSHIDEPROGRESS();
                                                                      $onformsenddata.="var sp=trim(response).split('|');
                                                                      //alert(response);
                                                                      if(sp[0]=='ok')
                                                                      {
                                                                           fn_dhtmlxmessage('','Ticket Number <b>'+myForm.getItemValue('txt_tiketnumber')+'</b> has been update');";
                                                                           $onformsenddata.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"cbo_idship");
                                                                           $onformsenddata.="myForm.setItemLabel('lblmeasure','Measure');";
                                                                           $onformsenddata.="myForm.enableItem('btnsave');";
                                                                           $onformsenddata.="myForm.disableItem('btnupdate');";
                                                                           $onformsenddata.="myForm.disableItem('btndelete');";
                                                                           $onformsenddata.="myForm.enableItem('cbo_idship');";
                                                                           $onformsenddata.="myForm.enableItem('txt_tiketnumber');";

                                                                           $onformsenddata.="fn_grid_loaddata();
                                                                      }
                                                                      else if(sp[0]=='failed')
                                                                      {fn_dhtmlxmessage('error','Error Found : '+sp[1]);}
                                                                      else
                                                                      {fn_dhtmlxmessage('error','Error Found : '+response);}
                                                                   });";
                                                                   $onbuttonclickbtnupdt.=$this->FN_DHTMLXWINDOWSHOWPROGESS($onformsenddata);
                            $onbuttonclickbtnupdt.="            }
                                                             }
                                                        });
                                                   }";
                            
                            $eventafterdeleted=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls,"cbo_idship");
                            //$eventafterdeleted.="document.getElementById('imgtiket').src='';";
                            $eventafterdeleted.="myForm.setItemLabel('lblmeasure','Measure');";
                            $eventafterdeleted.="myForm.enableItem('btnsave');";
                            $eventafterdeleted.="myForm.disableItem('btnupdate');";
                            $eventafterdeleted.="myForm.disableItem('btndelete');";
                            $eventafterdeleted.="fn_grid_loaddata();";
                            $onbuttonclickbtndeleted=$this->FN_DHTMLXREMARKFORDELETED2("obj_tra","tra_cargticket","myForm.getItemValue('txt_id')","myForm.getItemValue('txt_tiketnumber')",$eventafterdeleted);

                            //$onbuttonclickbtncancel="document.getElementById('imgtiket').src='';";
                            $onbuttonclickbtncancel="myForm.setItemLabel('lblmeasure','Measure');";
                            $onbuttonclickbtncancel.=$this->FN_DHTMLXFORMCLEAR2("myForm",$arrayControls);
                            $onbuttonclickbtncancel.="myForm.enableItem('btnsave');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btnupdate');";
                            $onbuttonclickbtncancel.="myForm.disableItem('btndelete');";
                            $onbuttonclickbtncancel.="myForm.enableItem('cbo_idship');";
                            $onbuttonclickbtncancel.="myForm.enableItem('txt_tiketnumber');";
//                            $onbuttonclickbtncancel.="myForm.disableItem('btndelete');";
                            $onbuttonclickbtncancel.="myForm.setItemFocus('cbo_idship');";

                            $onbuttonclickbtncancel.="myForm.setItemFocus('cbo_idship');";
                            $onbuttonclickbtncancel.="fn_grid_loaddata();";

                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnsave"=>"$onbuttonclickbtnsave",
                                                                                "btnupdate"=>"$onbuttonclickbtnupdt",
                                                                                "btndelete"=>"$onbuttonclickbtndeleted",
                                                                                "btncancel"=>"$onbuttonclickbtncancel"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txt_totalprice"=>"$onobjkeydown",
                                                                                "txt_quantity"=>"$onobjtxtquantity",                                                          
                                                          )
                                                )
                            );


                            $this->FN_DHTMLXFORMEVENT("myForm",$arrevent);

                            echo "myForm.getCombo('cbo_idship').attachEvent('onKeyPressed',function(keycode){
                                      if(keycode==null){return false;}
                                      if(keycode==13){".$onkeypresscboshipname."}
                                  });";
                            echo "myForm.getCombo('cbo_idrouteend').attachEvent(\"onKeyPressed\", function(keycode){
                                      if(keycode==13){
                                          fn_showcategory();   
                                      }
                                  });";

                            // === FORM SEARCH AND GRID
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"fieldset","width"=>"580","label"=>"Search",
                                                     "list"=>array(
                                                                      array("type"=>"input","name"=>"txtsearch","label"=>"Insert Your Keyword","info"=>"true","inputWidth"=>"240",
                                                                            "tooltip"=>"Insert Your Keyword","maxLength"=>"","className"=>"txtleft","labelWidth"=>"210"),
                                                                      array("type"=>"newcolumn"),
                                                                      array("type"=>"button","name"=>"btnfind","value"=>"","className"=>"button_find","offsetLeft"=>"10","offsetTop"=>"0")
                                                     )
                                              )
                            );

                            $this->FN_DHTMLXFORM("myFormsearch","divcargticketsearch",$formdata,true);
                            $ontxtsearchkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();myFormsearch.setItemFocus('btnfind');}";
                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnfind"=>"fn_grid_loaddata()"
                                                          )
                                                ),
                                                array("eventname"=>"onchange","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                            //"cbo_idrouteend"=>"alert('t');",
                                                                            
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                            "txtsearch"=>"$ontxtsearchkeydown"
                                                          )
                                                )
                            );
                            $this->FN_DHTMLXFORMEVENT("myFormsearch",$arrevent);

                            $grdinit=array("gridname"=>"dgcargticket","divtarget"=>"div_dgcargticket","divpaging"=>"div_dgcargticketpaging",
                                           "paginglimit"=>PAGE_LIMIT,"showloader"=>"true");
                            $gridproperties=array("id"=>"right,ro,96,right,int,false,true",
                                                  "P.&nbsp;&nbsp;"=>"center,ro,35,center,str,false",
                                                  "No.&nbsp;"=>"right,ro,35,center,int,false",
                                                  "Ship Name"=>"left,ro,140,left,str,false",
                                                  "Ticket Number"=>"center,ro,130,center,str,false",
                                                  "Voyage Number"=>"center,ro,130,center,str,false",
                                                  "Passenger Name"=>"left,ro,262,left,str,false",
                                                  "ID Card"=>"center,ro,136,center,str,false",
                                                  "Dates"=>"center,ro,120,center,date,false",
                                                  "From"=>"left,ro,112,left,str,false",
                                                  "Category"=>"left,ro,144,left,str,false",                                                  
                                                  "Price&nbsp;"=>"right,ro,120,right,int,false",
                                                  "Quantity&nbsp;"=>"right,ro,144,right,int,false",
                                                  "Weight&nbsp;"=>"right,ro,144,right,int,false",
                                                  "Total&nbsp;"=>"right,ro,120,right,int,false");
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            $arrfields=array("txt_id"=>"","cbo_idship"=>"idship|shipname","txt_tiketnumber"=>"","txt_voynumber"=>"",
                                             "txt_cargodescription"=>"","txt_passengername"=>"","cbo_idcardtype"=>"idcardtype|cardtype","txt_cardnumber"=>"","txt_dates"=>"",
                                             "cbo_idroutestart"=>"idroutestart|routenamestart","cbo_idrouteend"=>"idrouteend|routenameend","txt_price"=>"",
                                             "txt_quantity"=>"","txt_totalprice"=>"","txt_modifcount"=>"","txt_units"=>"","txt_picdata"=>"","txt_idcategory"=>"","txt_weight"=>"");
                            $otherevent="if(spres[15]>1)";
                            $otherevent.="{";
                            $otherevent.="    fn_dhtmlxmessage('error','I\'m sorry you cannot edit this record, only twice for editing! or Cancel this request to your administrator');return false;";
                            $otherevent.="}";
                            $otherevent.="myForm.disableItem('cbo_idship');";
                            $otherevent.="myForm.disableItem('txt_tiketnumber');";
                            $otherevent.="myForm.setItemFocus('txt_voynumber');";
                            $otherevent.="myForm.setItemLabel('lblmeasure',spres[15]);";
                            //$otherevent.="document.getElementById('imgtiket').src='uploads/temps/'+spres[16];";
                            
                            $this->FN_DHTMLXGRIDVIEWTOFORMNEW("dgcargticket","myForm","tbl_cargo_tiket",0,$arrfields,"",$otherevent);
            echo "    }

                      function fn_printtraold(tiketnumber,idtransaction,idcategory){
                         var sp=ajaxuridhtmlx('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cargticket")."&task=".$this->enc("printtra")."&idtransaction='+idtransaction+'&txt_tiketnumber='+tiketnumber+'&txt_category='+idcategory).split('|');
                         //alert(tiketnumber+' '+idtransaction+' '+idcategory);
                         //alert(sp[1]);
                         if(sp[0]=='ok'){
                            fn_dhtmlxmessage('','Tiket Number '+tiketnumber+' has been printed');
                         }
                      } 

                      function fn_printtra(tiketnumber,idtransaction){
                        if(!tiketnumber){
                            fn_dhtmlxmessage('error','Mohon maaf Nomor Tiket tidak terdaftar!');return false;
                        }
                        // alert(sepnumber);
                        var url='?ajax=1&sec=1&option=".$this->enc("obj_tra")."&obj=".$this->enc("tra_cargticket")."&task=".$this->enc("printtra")."&idtransaction='+idtransaction+'&tiketnumber='+tiketnumber;
                        dwins=new dhtmlXWindows();
                        dwins.createWindow(\"winprintticket\",0,0,screen.width-400,screen.height-350);
                        dwins.window('winprintticket').progressOn();
                        dwins.window(\"winprintticket\").setText(\"PRINT CARGO TICKETING\");
                        dwins.window(\"winprintticket\").attachURL(url);
                        dwins.window('winprintticket').setModal(true);
                        dwins.window('winprintticket').progressOff();
                      } 

                      function fn_showcategory()
                      {
                          if(myForm.getItemValue('cbo_idroutestart')==''){
                             fn_dhtmlxmessage('error','Please choice route arrival first ! ');myForm.setItemFocus('cbo_idroutestart');return false;
                          }
                          if(myForm.getItemValue('cbo_idrouteend')==''){
                             fn_dhtmlxmessage('error','Please choice route destination first ! ');myForm.setItemFocus('cbo_idrouteend');return false;
                          }
                         
                          dwins=new dhtmlXWindows();dwins.attachViewportTo(\"winVP\");
                          var idroutestart=myForm.getItemValue('cbo_idroutestart');
                          var idrouteend=myForm.getItemValue('cbo_idrouteend');
                          var url='?ajax=1&sec=1&option=".$this->enc("obj_tra")."&obj=".$this->enc("tra_cargticket")."&task=".$this->enc("getcategory")."&idroutestart='+idroutestart+'&idrouteend='+idrouteend;";
                          $rtn="dwins.createWindow(\"winbook\",0,0,850,580);";
                          $rtn.="dwins.window('winbook').denyResize();";
                          $rtn.="dwins.window('winbook').progressOn();";
                          $rtn.="dwins.window(\"winbook\").setText(\"Get Cargo Item\");";
                          $rtn.="dwins.window(\"winbook\").attachURL(url);";
                          $rtn.="dwins.window('winbook').setModal(true);";
                          $rtn.="dwins.window('winbook').progressOff();";
                          echo $rtn;
            echo "    }
            
                      function fn_getCategory(idcategory,categorydescription,price,measure){
                         myForm.setItemValue('txt_idcategory',idcategory);
                         myForm.setItemValue('txt_cargodescription',categorydescription);
                         myForm.setItemLabel('lblmeasure',measure);
                         var qty=myForm.getItemValue('txt_quantity');
                         if(qty<=0){qty=1;}
                         myForm.setItemValue('txt_quantity',qty);
                         myForm.setItemValue('txt_price',price);
                         var totalprice=qty*price;
                         myForm.setItemValue('txt_totalprice',totalprice);
                         myForm.setItemFocus('txt_quantity');
                         dwins.unload();
                         
                      }
                      
                      function fn_showCamera()
                      {
                          dwins=new dhtmlXWindows();dwins.attachViewportTo(\"winVP\");
                          var picdata=myForm.getItemValue('txt_picdata');
                          var url='?ajax=1&sec=1&option=".$this->enc("obj_tra")."&obj=".$this->enc("tra_cargticket")."&task=".$this->enc("getpic")."&picdata='+picdata;";
                          $url="?ajax=1&sec=1&option=".$this->enc("obj_tra")."&obj=".$this->enc("tra_newticket")."&task=".$this->enc("getpic");
                          $this->FN_DHTMLXWINDOWSURL("dwins","winpic","Get Picture","50","50","560","400",$url,false,true);
                          
            echo "    }

                      function fn_getPicName(filename){
                         myForm.setItemValue('txt_picdata',filename);
                         myForm.setItemFocus('txt_dates');
                         dwins.unload();
                         document.getElementById('imgtiket').src='uploads/temps/'+filename;
                         
                      }
                      
                      function fn_grid_loaddata()
                      {
                          var keyword=myFormsearch.getItemValue('txtsearch');
                          url=fn_dhtmlxloadobject('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cargticket")."&task=".$this->enc("grdrecord")."&keyword='+keyword);
                          //alert(url);
                          dgcargticket.clearAndLoad(url);";
            echo "    }
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");
            
            echo "<div id=\"winVP\" style=\"height:1020px;\">";
            echo "<table width=\"98%\" border=\"0\">
                      <tr>
                          <td>";
                              $url="location.reload();";
                              $myuri=array("<b>CARGO TICKETING INFORMATION</b>"=>"$url|id='a_cargticket'");
                              echo $this->MULTIPLE_LINK($myuri);
            echo "       </td>
                      </tr>
                      <tr>
                          <td>
                              <div id=\"divcargticketcontent\" style=\"margin-left:0px;\">
                                  <table width=\"98%\" border=\"0\">
                                      <tr>
                                          <td width=\"45%\" valign=\"top\">
                                              <div id=\"divcargticketnew\" style=\"\"></div>
                                          </td>
                                          <td valign=\"top\">
                                              <table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\">
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <div id=\"divcargticketsearch\" style=\"width:560px;margin-top:-6px;\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgcargticket\" style=\"height:380px;width:580px;overflow:hidden\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr>
                                                      <td>
                                                          <div id=\"div_dgcargticketpaging\" style=\"width:598px;margin-top:1px\"></div>
                                                      </td>
                                                  </tr>
                                                  <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-220px;margin-left:287px;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                                                  <tr>
                                                      <td valign=\"top\">
                                                          <table>
                                                              <tr>
                                                                  <td style='color:red;font-weight:bold;'>Notes</td>
                                                                  <td>:</td>
                                                                  <td>- Click Row For Edit</td>
                                                              </tr>
                                                          </table>
                                                      </td>
                                                  </tr>
                                              </table>
                                          </td>
                                      </tr>
                                  </table>
                              </div>
                          </td>
                      </tr>
                  </table>
                  </div>";
            echo $this->FN_CREATEFOOTER();

        }

        public function fn_grdrecord()
        {
            $keyword=$_REQUEST["keyword"];
            header("Content-type: text/xml");
            echo('<?xml version="1.0" encoding="utf-8"?>');
            echo "<rows id='0'>";

            $qry="select id,idship,shipname,tiketnumber,tiketnumberpassenger,voynumber,inctiket,idcategory,cargodescription,passengername,
                         idcardtype,cardtype,cardnumber,dates,idroutestart,routenamestart,idrouteend,
                         routenameend,price,quantity,weight,totalprice,units,modifcount
                  from ".DB_APORTIL.".tbl_cargo_tiket
                  where (tiketnumber like '%$keyword%' or voynumber like '%$keyword%' or passengername like '%$keyword%') and isdeleted=0
                  order by id asc limit ".TBL_PAGE_LIMIT;

            $rs=$this->selectquery($qry);
            if($rs)
            {
               $i=1;
               while($row=$this->data_row_array($rs))
               {
                   $color='black';
                   if($row["modifcount"]>=2){$color='grey';}
                   $print="&lt;a href=\"javascript:void(0)\" title=\"Reprint Ticket Number ".$row["tiketnumber"]."\" style=\"text-decoration:none\" onClick=\"fn_printtra('".$row['tiketnumber']."','".$row['id']."','".$row['idcategory']."')\"/&gt;";
                   $print.="&lt;img src='themes/images/print.gif' /&gt;";
                   $print.="&lt;a/&gt;";
                   echo ("<row id='".$row["id"]."'>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                         print("<cell>$print</cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$i."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["shipname"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["tiketnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["voynumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["passengername"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["cardtype"]."/".$row["cardnumber"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".date("Y-m-d",strtotime($row["dates"]))."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["routenamestart"]." to ".$row['routenameend']."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["cargodescription"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["price"]." \$</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["quantity"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["weight"]."</span>"."]]></cell>");
                        print("<cell><![CDATA["."<span style='color:$color'>".$row["totalprice"]." \$</span>"."]]></cell>");
                   echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }
        
        public function fn_saverecord($isupdate=null)
        {
            $year=date("Y");$id=0;
            if($isupdate){$id=$_REQUEST['txt_id'];}
            $idship=$_REQUEST['cbo_idship'];
            $sp=$this->fn_get_fieldsvalues("shipname,shipnumber","select shipname,shipnumber from ".DB_APORTIL.".tbl_ship_information where id='$idship'");
            $shipnumber=$sp[1];$shipname=$sp[0];                        
            $tiketnumber=$_REQUEST['txt_tiketnumber'];
            $tiketnumberpassenger=$_REQUEST['txt_tiketnumberpassenger'];
            $voynumber=$_REQUEST['txt_voynumber'];
            $inctiket=$_REQUEST['txt_inctiket'];
            $idcategory=$_REQUEST['txt_idcategory'];
            $spcategory=$this->fn_get_fieldsvalues("id,cargocategory,cargodescription,idunits,units","select id,cargocategory,cargodescription,idunits,units from tbl_cargoprice_information where id='$idcategory'");
            $cargocategory=$spcategory[1];
            $cargodescription=$spcategory[2];
            $idunits=$spcategory[3];
            $units=$spcategory[4];

            $inctiket=0;$inctiketyear=0;
            if(!$isupdate){
                $qry="select count(id) incnumber from ".DB_APORTIL.".tbl_cargo_tiket 
                      where isdeleted=0 and years='$year' and status>=0";
                $inctiketyear=$this->fn_get_fieldvalue("incnumber",$qry);            
                $inctiketyear+=1;
                
                $qry="select count(id) incnumber from ".DB_APORTIL.".tbl_cargo_tiket
                      where idship='$idship' and isdeleted=0 and cast(dates as date)=cast(now() as date) and idroutestart='$idroutestart' and idrouteend='$idrouteend' and status>=0";
                $inctiket=$this->fn_get_fieldvalue("incnumber",$qry);            
                $inctiket+=1;

                //$inctiket=$this->fn_get_fieldvalue("incnumber","select count(id) incnumber from ".DB_APORTIL.".tbl_cargo_tiket where isdeleted=0 and years='$year'");                            
                //$inctiket+=1;
            }

            $passengername=$_REQUEST['txt_passengername'];
            $idcardtype=$_REQUEST['cbo_idcardtype'];
            $cardtype=$this->fn_get_fieldvalue("cardtype","select cardtype from ".DB_APORTIL.".tbl_passenger_identifycardtype where id='$idcardtype'");
            $cardnumber=$_REQUEST['txt_cardnumber'];
            $picdata="aportildefault.png";//$_REQUEST['txt_picdata'];            
            $dates=$_REQUEST['txt_dates'];
            $idroutestart=$_REQUEST['cbo_idroutestart'];
            $routenamestart=$this->fn_get_fieldvalue("routename","select routename from ".DB_APORTIL.".tbl_route_information where id='$idroutestart'");
            $idrouteend=$_REQUEST['cbo_idrouteend'];
            $routenameend=$this->fn_get_fieldvalue("routename","select routename from ".DB_APORTIL.".tbl_route_information where id='$idrouteend'");
            
            $price=$_REQUEST['txt_price'];
            $quantity=$_REQUEST['txt_quantity'];
            $weight=$_REQUEST['txt_weight'];
            $totalprice=$_REQUEST['txt_totalprice'];

            $qry="insert into ".DB_APORTIL.".tbl_cargo_tiket 
                  set idship='$idship',tiketnumber='$tiketnumber',tiketnumberpassenger='$tiketnumberpassenger',voynumber='$voynumber',inctiket='$inctiket',inctiketyear='$inctiketyear',
                      idcategory='$idcategory',cargocategory='$cargocategory',cargodescription='$cargodescription',shipnumber='$shipnumber',shipname='$shipname',
                      passengername='$passengername',idcardtype='$idcardtype',cardtype='$cardtype',cardnumber='$cardnumber',picdata='$picdata',dates='$dates',
                      idroutestart='$idroutestart',routenamestart='$routenamestart',idrouteend='$idrouteend',routenameend='$routenameend',idunits='$idunits',status=0,
                      units='$units',price='$price',quantity='$quantity',weight='$weight',totalprice='$totalprice',years='$year',iby='".$_SESSION['nik']."',idt=now()";
            if($isupdate)
            {
                $qry="update ".DB_APORTIL.".tbl_cargo_tiket
                      set idship='$idship',tiketnumber='$tiketnumber',tiketnumberpassenger='$tiketnumberpassenger',voynumber='$voynumber',
                          idcategory='$idcategory',cargocategory='$cargocategory',cargodescription='$cargodescription',shipnumber='$shipnumber',shipname='$shipname',
                          passengername='$passengername',idcardtype='$idcardtype',cardtype='$cardtype',cardnumber='$cardnumber',picdata='$picdata',dates='$dates',
                          idroutestart='$idroutestart',routenamestart='$routenamestart',idrouteend='$idrouteend',routenameend='$routenameend',idunits='$idunits',
                          units='$units',price='$price',quantity='$quantity',weight='$weight',totalprice='$totalprice',modifcount=modifcount+1,uby='".$_SESSION['nik']."',udt=now()
                      where id='$id'";
            }

            if($this->executequery($qry))
            {
                if(!$id){$id=mysql_insert_id();}
                $qry="insert into ".DB_APORTIL.".tbl_cargo_tiket_log 
                        set idcargoticket='$id',dates='".date("Y-m-d")."',status=0,remarkstatus='Submitted',
                            remark='',iby='".$_SESSION['nik']."',idt=now()";
                if(!$this->fn_row_isexists("select id from ".DB_APORTIL.".tbl_cargo_tiket_log where idcargoticket='$id' and status=0 order by id desc limit 1")){
                    if($this->executequery($qry)){
                       
                    }
                }
                echo "ok|$id";return true;
    
                // if(!$isupdate)
                // {
                //     $idtransaction=mysql_insert_id();
                    // $rtn=$this->fn_printtra($idtransaction);
                    // echo $rtn;
                // }
                // else
                // {
                    // echo "ok|";return true;    
                // }
            }
        }
        public function fn_updrecord(){$this->fn_saverecord(true);}

        public function fn_delrecord()
        {
            $id=$_REQUEST['txt_id'];
            $remark=$_REQUEST['txtremark'];
            $qry="update ".DB_APORTIL.".tbl_cargo_tiket
                  set isdeleted=1,remark='$remark',dby='".$_SESSION['nik']."',ddt=now()
                  where id='$id'";
            if($this->executequery($qry)){echo "ok|";return true;}
            else
            {
              echo "failed|".$this->getErrorQuery();
            }
        }
        
        public function fn_printtra($idtransaction=''){
            date_default_timezone_set("Asia/Bangkok");
            include("lib/mpdf/mpdf.php");        
            $top=10;
            // $mpdf = new mPDF('c', 'A4', '', '', 5, 5, $top, 10, 3, 3, 'P');
            $mpdf = new mPDF('utf-8', array(250,120) ,'', '', 5, 5, 5);
            $mpdf->SetDisplayMode('fullpage');
            $tiketnumber=$_REQUEST['tiketnumber'];
            
            // LOAD a stylesheet
            $stylesheet = file_get_contents('lib/mpdf/css/mpdfstyleA4.css');

            $html="<div style=\"position: fixed; left: 0mm; middle: 160mm; rotate: -90;\">";
            $html.="    <div style=\"font-size:12pt;font-weight:bold\">".str_repeat("&nbsp;",6)."$tiketnumber</div>";
            $html.="    <div><barcode code=\"$tiketnumber\" type=\"C39\" class=\"barcode\" size=\"1.5\"/></div>";
            $html.="</div>";
            
            $qry="select * from ".DB_APORTIL.".tbl_cargo_tiket where id='".$_REQUEST['idtransaction']."' and isdeleted=0";
            $rs=$this->selectquery($qry);
            $rw=$this->data_row_array($rs);
            $html.="<table width=\"80%\" style=\"margin-left:30px;padding-top:20px\" align=\"center\" border=\"0\">";
            $html.="<tr>";        
            $html.="     <td style=\"font-size:14pt\" colspan=\"6\">TICKET ".$rw['shipname']."</td>";
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\" width=\"110\">Typu</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\">Cargo</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:30px\"></td>";
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Nome</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".$rw['passengername']."</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Destino</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
                         $destination=$rw['routenamestart']."-".$rw['routenameend'];

            $html.="     <td style=\"font-size:12pt;padding-top:10px\">".$destination."</td>";

            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Data</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".date("d/m/Y H:i:s a",strtotime($rw['dates']))."</td>";

            
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Voy</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".$rw['voynumber']."</td>";
            
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"140\">Tipo do Veiculo</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".$rw['cargodescription']."</td>";
            
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Qty</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".$rw['quantity']."</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">No</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">".$rw['inctiket']."</td>";            
            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"110\">Preco</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\">US$ ".$rw['price']."</td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td valign=\"top\" style=\"font-size:12pt;padding-top:10px\"></td>";

            $html.="</tr>";
            $html.="<tr>";        
            $html.="     <td style=\"font-size:12pt;padding-top:10px\" width=\"110\">Total Preco</td>";
            $html.="     <td style=\"font-size:12pt;padding-top:10px\" width=\"2\">:</td>";
                         $totalprice=$rw['price']*$rw['quantity'];
            $html.="     <td style=\"font-size:12pt;padding-top:10px\">US$ ".$totalprice."</td>";
            $html.="     <td style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td style=\"font-size:12pt;padding-top:10px\"></td>";
            $html.="     <td style=\"font-size:12pt;padding-top:10px\"></td>";            
            $html.="</tr>";
            $html.="<tr>";
            $html.="    <td colspan=\"6\" align=\"center\" style=\"padding-top:20\">-- BOARDING PASS --</td>";
            $html.="</tr>";
            $html.="<tr>";
            $html.="    <td colspan=\"6\" align=\"left\" style=\"padding-top:10\">Nota  : O bilhete que já foram comprados não podem ser cancelados</td>";
            $html.="</tr>";
            $html.="</table>";


            $mpdf->WriteHTML($html);

            // $secureofletter=$this->enc("ticket_".$tiketnumber."_".date("His"));
            // $mpdf->setFooter('{PAGENO} of {nbpg} pages || Secure ID : '.$secureofletter) ;

            $mpdf->output();
        }

        public function fn_printtraold($idtransaction='')
        {
            $tiketnumber=$_REQUEST['txt_tiketnumber'];
            if(!$idtransaction){
                $idtransaction=$_REQUEST['idtransaction'];
            }
            $idcategory=$_REQUEST['txt_idcategory'];
            $spcategory=$this->fn_get_fieldsvalues("id,cargocategory,cargodescription,idunits,units","select id,cargocategory,cargodescription,idunits,units from tbl_cargoprice_information where id='$idcategory'");
            $cargocategory=$spcategory[1];
            $cargodescription=$spcategory[2];
            $ipprintclient=$this->get_client_ip();
            $jobgroup="TICV";
            
            if($cargocategory!=='Veiculos'){$jobgroup="TICNV";}
            $jobgroup="TICV";
            $qry="insert into ".DB_APORTIL.".tbl_printjob 
                  set idtransaction='$idtransaction',ipprintclient='$ipprintclient',idemployee='".$_SESSION['idemployee']."',nik='".$_SESSION['nik']."',
                      employeename='".$_SESSION['employeename']."',jobgroup='$jobgroup',jobremark='$tiketnumber',iby='".$_SESSION['nik']."',idt=now()";
            //echo "ok|".$qry;
            $qrychk="select id from ".DB_APORTIL.".tbl_printjob where ipprintclient='$ipprintclient' order by id desc limit 1";
            if($this->fn_row_isexists($qrychk))
            {
                $id=$this->fn_get_fieldvalue("id",$qrychk);
                $qry="update ".DB_APORTIL.".tbl_printjob 
                      set idtransaction='$idtransaction',ipprintclient='$ipprintclient',idemployee='".$_SESSION['idemployee']."',nik='".$_SESSION['nik']."',
                          employeename='".$_SESSION['employeename']."',jobgroup='$jobgroup',jobremark='$tiketnumber',uby='".$_SESSION['nik']."',udt=now()
                      where id='$id'";
            }
            
            $rtn="failed|Print Tiket $tiketnumber failed, please contact your administrator. Thank you";
            if($this->executequery($qry))
            {
                $rtn="ok|";                
            }
            echo $rtn;
            return $rtn;
        }

        
        public function fn_getticket(){
            // $valticket="";
            // for($i=1;$i<=8;$i++){
            //     $valticket.=rand(0,9);
            // }
            $valticket=date("YmdHis");
            $sql="select tiketnumber from ".DB_APORTIL.".tbl_cargo_tiket where tiketnumber='".$valticket."' order by id desc limit 1";
            if($this->fn_row_isexists($sql)){
                $this->fn_getticket();   
            }
            return $valticket;
        }
        
        public function fn_getheaderinfo(){
            $idship=$_REQUEST['idship'];
            $tiketnumber=$this->fn_getticket();
            echo "ok|".$tiketnumber;
        }
  
              
        public function fn_getpic(){
            $picdata=$_REQUEST['picdata'];
            if($picdata){unlink('uploads/temps/'.$picdata);}
            
            echo $this->FN_CREATEHEADER();
            echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" />";
            echo "<script src=\"".PATH_THEME."/js/camupload.js\"></script>";
            echo "<script>
                      function initObject()
                      {}   
                      function fn_setImage(filename){
                          //window.opener.document.getElementById(\"blah_ReturnValue\").value = rv;
                          window.parent.fn_getPicName(filename);
                          
                      }";
            echo "</script>
                  <style>
                     html, body {
                        width: 100%;
                        height: 100%;
                        overflow: hidden;
                        margin: 0px;
                        /*background-color: #EBEBEB;*/
                    }
                  </style>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");
//            echo "<div id=\"vid-controls\" style=\"margin-left:-80px\">
//                    
//                  </div>";
            echo "<video id=\"vid-show\" autoplay height=\"400\"></video>
                  <div style=\"position:absolute;z-index:999;margin-top:-18%;margin-left:90%\">
                      <a href=\"javascript:void(0)\" id=\"vid-take\" style=\"color:white\"><i class=\"fa fa-camera\" style=\"font-size:26pt;\"></i></a>
                  </div>
                  <div id=\"vid-canvas\" style=\"\"></div>";
            //echo "<button onClick='fn_setImage()'>Show Parent</button>";
            echo $this->FN_CREATEFOOTER();
            
            
        }
        
        public function fn_getcategory()
        {
            echo $this->FN_CREATEHEADER();
            $idroutestart=$_REQUEST['idroutestart'];
            $idrouteend=$_REQUEST['idrouteend'];
            echo "<style>
                      .besidebtnsave {margin-left:110px;margin-top:-6px;position:absolute;}
                      .besidebtnupdate {margin-left:212px;margin-top:-6px;position:absolute;}
                      .besidebtnsubmit {margin-left:292px;margin-top:-6px;position:absolute;}
                      .besidebtndelete {margin-left:310px;margin-top:-6px;position:absolute;}
                      .besidelabel {margin-left:300px;margin-top:-6px;position:absolute;}
                  </style>";
            echo "<script>
                       var myForm,myFormsearch,dgcargoprice;
                       function initObject()
                       {";
                            // === FORM SEARCH AND GRID
                            $formdata=array(
                                              array("type"=>"settings","position"=>"label-left","labelWidth"=>"180","inputWidth"=>"120"),
                                              array("type"=>"fieldset","width"=>"780","label"=>"Search",
                                                     "list"=>array(
                                                                      array("type"=>"input","name"=>"txtsearch","label"=>"Insert Your Keyword","info"=>"true","inputWidth"=>"240",
                                                                            "tooltip"=>"Insert Your Keyword","maxLength"=>"","className"=>"txtleft","labelWidth"=>"210"),
                                                                      array("type"=>"newcolumn"),
                                                                      array("type"=>"button","name"=>"btnfind","value"=>"","className"=>"button_find","offsetLeft"=>"10","offsetTop"=>"5")
                                                     )
                                              )
                            );

                            $this->FN_DHTMLXFORM("myFormsearch","divcargopricesearch",$formdata,true);
                            $ontxtsearchkeydown="if(ev.keyCode==13){ev.stopPropagation();ev.preventDefault();myFormsearch.setItemFocus('btnfind');}";
                            $arrevent=array(
                                                array("eventname"=>"onbuttonclick","eventparam"=>"id,val",
                                                          "switch"=>array(
                                                                                "btnfind"=>"fn_grid_loaddata()"
                                                          )
                                                ),
                                                array("eventname"=>"onkeydown","eventparam"=>"inp,ev,id,val",
                                                          "switch"=>array(
                                                                                "txtsearch"=>"$ontxtsearchkeydown"
                                                          )
                                                )
                            );
                            $this->FN_DHTMLXFORMEVENT("myFormsearch",$arrevent);

                            $grdinit=array("gridname"=>"dgcargoprice","divtarget"=>"div_dgcargoprice","divpaging"=>"div_dgcargopricepaging","paginglimit"=>PAGE_LIMIT,
                                           "showloader"=>"true");
                            $gridproperties=array("id"=>"right,ro,96,right,int,false,true",
                                                  "No.&nbsp;"=>"right,ro,35,center,int,false",
                                                  "Category"=>"left,ro,144,left,str,false",
                                                  "Category Description"=>"left,ro,200,left,str,false",
                                                  "Destination"=>"left,ro,160,left,str,false",
                                                  "Prices&nbsp;"=>"right,ro,110,right,int,false",
                                                  "Priceshide&nbsp;"=>"right,ro,110,right,int,false,true",
                                                  "Units&nbsp;"=>"right,ro,110,right,str,false",                                                  
                                                  "Years"=>"center,ro,110,center,int,false");
                            $this->FN_DHTMLXGRIDVIEW($grdinit,$gridproperties,"bricks","","fn_grid_loaddata()");
                            echo "dgcargoprice.attachEvent(\"onRowDblClicked\", function(rId,cInd){
                                     var idcategory=dgcargoprice.cells(rId,0).getValue();
                                     var categorydescription=dgcargoprice.cells(rId,3).getValue();
                                     var price=dgcargoprice.cells(rId,6).getValue();
                                     var units=dgcargoprice.cells(rId,7).getValue();
                                     window.parent.fn_getCategory(idcategory,categorydescription,price,units);
                                  });";
            echo "    }

                      function fn_grid_loaddata()
                      {
                          var keyword=myFormsearch.getItemValue('txtsearch');
                          url=fn_dhtmlxloadobject('".$this->enc("obj_tra")."','obj=".$this->enc("tra_cargticket")."&task=".$this->enc("grdcategory")."&idroutestart=$idroutestart&idrouteend=$idrouteend&keyword='+keyword);
                          //alert(url);
                          dgcargoprice.clearAndLoad(url);";
            echo "    }
                   </script>";
            echo $this->FN_CLOSEHEADER();
            echo $this->FN_CREATEBODY("e8f1ff");

            echo "<table width=\"98%\" border=\"0\">
                      <tr>
                          <td valign=\"top\">
                              <table width=\"98%\" cellpadding=\"0\" cellspacing=\"0\">
                                  <tr>
                                      <td valign=\"top\">
                                          <div id=\"divcargopricesearch\" style=\"width:760px;margin-top:-6px;\"></div>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <div id=\"div_dgcargoprice\" style=\"height:380px;width:780px;overflow:hidden\"></div>
                                      </td>
                                  </tr>
                                  <tr>
                                      <td>
                                          <div id=\"div_dgcargopricepaging\" style=\"width:598px;margin-top:1px\"></div>
                                      </td>
                                  </tr>
                                  <tr><td valign=\"top\"><div id=\"loadergrid\" style=\"z-index:100;position:absolute;margin-top:-220px;margin-left:387px;display:visibility;\">".$this->CREATE_IMAGE("loadergrid.gif")."</div></td></tr>
                                  <tr>
                                      <td valign=\"top\">
                                          <table>
                                              <tr>
                                                  <td style='color:red;font-weight:bold;'>Notes</td>
                                                  <td>:</td>
                                                  <td>- Double Click Row For Get Data</td>
                                              </tr>
                                          </table>
                                      </td>
                                  </tr>
                              </table>
                          </td>
      
                      </tr>
                  </table>";
            echo $this->FN_CREATEFOOTER();

        }
        
        public function fn_grdcategory()
        {
            $idroutestart=$_REQUEST["idroutestart"];
            $idrouteend=$_REQUEST["idrouteend"];
            $keyword=$_REQUEST["keyword"];
            header("Content-type: text/xml");
            echo('<?xml version="1.0" encoding="utf-8"?>');
            echo "<rows id='0'>";

            $qry="select id,idcategory,cargocategory,cargodescription,routenamestart,routenameend,units,prices,years
                  from ".DB_APORTIL.".tbl_cargoprice_information
                  where (cargodescription like '%$keyword%' or cargocategory like '%$keyword%') and (idroutestart='$idroutestart' and idrouteend='$idrouteend') and isdeleted=0
                  order by id asc limit ".TBL_PAGE_LIMIT;

            $rs=$this->selectquery($qry);
            if($rs)
            {
               $i=1;
               while($row=$this->data_row_array($rs))
               {
                  echo ("<row id='".$row["id"]."'>");
                        print("<cell><![CDATA[".$row["id"]."]]></cell>");
                        print("<cell><![CDATA[".$i."]]></cell>");
                        print("<cell><![CDATA[".$row["cargocategory"]."]]></cell>");
                        print("<cell><![CDATA[".$row["cargodescription"]."]]></cell>");
                        $destination=$row['routenamestart']."-".$row['routenameend'];
                        print("<cell><![CDATA[".$destination."]]></cell>");
                        print("<cell><![CDATA[".$row["prices"]." \$]]></cell>");                        
                        print("<cell><![CDATA[".$row["prices"]."]]></cell>");                        
                        print("<cell><![CDATA[".$row["units"]."]]></cell>");
                        print("<cell><![CDATA[".$row["years"]."]]></cell>");
                  echo ("</row>");
                  $i+=1;
               }
            }
            else
            {
                  echo mysql_errno().": ".mysql_error()." at ".__LINE__." line in ".__FILE__." file<br>";
            }
            echo "</rows>";

        }
    }
?>