<?php
$project_id = (int)$_REQUEST['project_id'];
?>
<input type="hidden" name="hid_file_id" id="hid_file_id" value="<?php echo $file_id; ?>">
<?php echo $form->hiddenField($model, 'id'); ?>
<?php echo $form->hiddenField($model, 'enquiry_id'); ?>
<?php echo $form->hiddenField($model, 'req_id'); ?>
<?php echo $form->hiddenField($model, 'title'); ?>
<?php //model->currency  = Yii::app()->user->company_base_currency;
//$model->exchange_rate = 1;
//echo $form->hiddenField($model, 'currency'); 
?>
<?php //echo $form->hiddenField($model, 'exchange_rate'); 
?>
<?php $model->supplier_type = 1;
echo $form->hiddenField($model, 'supplier_type', array('value' => '1'));
if (empty($model->start_text)) {
    $model->start_text = "Please supply the following items at the earliest.";
}
echo $form->hiddenField($model, 'start_text');
echo $form->hiddenField($model, 'shipping_by', array('value' => '6'));
echo $form->hiddenField($model, 'shipping_address');
echo $form->hiddenField($model, 'expected_delivery_date');
?>
<ul class="clearfix form-ul material">

    <!--<li class="col-sm-2">
                    <?php /*echo $form->textField($model, 'title', array('title' => 'File Title ','data-label'=>'File Title <span style="color:red;"></span>')); ?>
            
      </li>
      <li class="col-sm-2">
          <?php 
          $supplier_type=1;
          if($model->supplier_type > 0){
              $supplier_type=$model->supplier_type;
          }
          echo $form->dropDownList($model, 'supplier_type', MasSupplierTypes::getSuppliertypes(), array('options' => array($supplier_type=>array('selected'=>true)),'class' => '', 'title' => 'Supplier Type','data-label'=>'Supplier Type','onChange' => 'getSupplierList(this.value)')); */ ?>
                      
                </li>-->

    <li class="col-sm-3" id="supplier_div">
        <?php
        // if ($enq_id > 0) {
        //     $arr_vendor = Vendor::getVendorsEnquiry($enq_id);
        //     $model->vendor_id = $vendor_id;
        // } else {
        //     $arr_vendor = Vendor::getVendorsByType(1);
        // }
        $arr_vendor = Vendor::getVendors();
        echo $form->dropDownList($model, 'vendor_id', $arr_vendor, array('class' => '', 'prompt' => '-select-', 'title' => 'Select Supplier *', 'data-label' => 'Supplier <span style="color:red;">*</span>', 'onChange' => 'getContactCurrencyDetails(this.value)', 'data-search' => true)); ?>

    </li>

    <div id="div_contact">
        <li class="col-sm-3">
            <?php
            if ($model->vendor_id > 0) {
                $vendor_id = $model->vendor_id;
            }
            if ($model->vendor_contact_id > 0) {
                $contact_name = $model->vendorcontact->person_name;
                echo $form->dropDownList($model, 'vendor_contact_id', VendorContact::getVendorContacts($vendor_id), array('options' => array($model->vendor_contact_id => array('selected' => true)), 'readonly' => true, 'data-label' => 'Contact <span style="color:red">*</span>'));
            } else {
                if ($vendor_id > 0) {
                    $arr_contacts = VendorContact::getVendorContacts($vendor_id);
                } else
                    $arr_contacts = array();
                echo $form->dropDownList($model, 'vendor_contact_id', $arr_contacts, array('readonly' => true, 'data-label' => 'Contact <span style="color:red">*</span>', 'prompt' => '--Select--'));
            }
            ?>
        </li>
    </div>
    
 <li class="col-sm-3" id="p_div">
        <div id="div_payment_term">
            <?php
            /*if (empty($model->payment_terms)) {
                $model->payment_terms = 1;
            }*/

            echo $form->dropDownList($model, 'payment_terms', MasPaymentTerm::getPaymentTerms(), array('class' => '', 'title' => 'Select Payment Terms', 'data-label' => 'Payment Terms <span style="color:red;">*</span> ')); ?>
        </div>


    </li>
   

    <li class="col-sm-3" id="">

        <?php echo $form->textField($model, 'order_date', array('class' => 'datepicker', 'title' => 'Order Date ', 'data-label' => 'Order Date')); ?>

    </li>
    <?php  if ((int) Yii::app()->user->haveLevelPermissionOn['master_level_permission_currency'] > 0 || Yii::app()->user->authorised_user == 1) {?>

    <li class="col-sm-3" id="c_div">
        <?php
        if ($model->isNewRecord) {
            if ($model->vendor_id > 0) {
                $model->currency = $model->vendor_details->vendor_currency_id;
            }
            else if($_REQUEST['project_id']){
                $model->currency = MasCurrency::model()->findByAttributes(array('isBaseCurrency' => 'Y'))->id;
            }
        }
        ?>

        <?php echo $form->dropDownList($model, 'currency', MasCurrency::getCurrency(), array('onChange' => 'getExchangeRate(this.id)', 'class' => 'tooltipster', 'title' => 'Currency', 'data-label' => 'Currency', 'prompt' => '--Select--', 'data-search' => true)); ?>



    </li>


    <li class="col-sm-3">
        <?php
        if ($model->id == 0) {
            if ($model->vendor_id > 0) {
                $model->exchange_rate = $model->vendor_details->currency_detail->exchange_rate;
            }
            else if($_REQUEST['project_id']){
                $model->exchange_rate = MasCurrency::model()->findByAttributes(array('isBaseCurrency' => 'Y'))->exchange_rate;
            }
        } ?>

        <?php
        if (Yii::app()->user->company_base_currency == $model->currency) {
            echo $form->textField($model, 'exchange_rate', array('class' => 'tooltipster', 'title' => 'Exchange Rate', 'data-label' => 'Exchange Rate', 'value' => $exchange_rate, 'readonly' => true));
        } else {
            echo $form->textField($model, 'exchange_rate', array('class' => 'tooltipster', 'title' => 'Exchange Rate', 'data-label' => 'Exchange Rate', 'value' => $exchange_rate));
        }
        ?>
    </li>
<?php }else {
            $model->currency = MasCurrency::base_currency_id(); 
          echo  $form->hiddenField($model, 'currency'); 
          $model->exchange_rate = MasCurrency::base_exchange_rate();
		     echo  $form->hiddenField($model, 'exchange_rate'); 
   }
   ?>

 
 <li class="col-sm-4">           

                    <?php
                    if($model->billing_address  == ''){
                        $masCompanyAddress  = $masCompanyAddress;
                    }
                    else{
                        $masCompanyAddress = $model->billing_address ;
                    }
                    
                    ?>
                    <?php echo $form->textArea($model, 'billing_address', array('title' => 'Address','data-label'=>'Address <span style="color:red;">*</span> ','data-search'=>'true'));  ?>

   

                </li>




 

    
    <li class="col-sm-4"></li>

    



</ul>
<?php
$cs = Yii::app()->getClientScript();


$cs->registerScript("loadUsers", '
        $(document).ready(function() {
            function getInvoiceTypePercentage(id)
            {
                if(id>0){
        
            jQuery.ajax({
                "dataType" : "json",
              "type"  : "post",
              "data"  : "id="+id,
              "url"  : $("#liveSite").val()+"index.php?r=masinvoicetype/getInvoiceTypeTax",
              "cache": false,
              "success":function(data){
                
                if(data.status=="success"){
                    
                    var tr_rowCount = $("#order_item_table tr").length;
                   
                    tr_rowCount  = tr_rowCount-2;
                    if(data.tax == "Y"){
                        
                        for (i = 0; i <= tr_rowCount; i++){
                            
                            var find_value  = $("#check_tax_purchaseOrder_"+i).val();                            
                            $("#PurchasingOrderStockItem_"+i+"_tax_percentage").val(find_value);
                        }
                        
                    }
                    else{
                        for (i = 0; i <= tr_rowCount; i++){
                            
                            $("#PurchasingOrderStockItem_"+i+"_tax_percentage").val(0);
                        }
                    }
                    calculatestockValues($("#order_item_table"));
                }
                else{
                    //$("#Order_vat_percentage").val();
                }
                
               }
            });
        
    }
            }
            var PurchasingOrder_invoice_type  = $("#PurchasingOrder_invoice_type").val();
            getInvoiceTypePercentage(PurchasingOrder_invoice_type);
            $("#PurchasingOrder_invoice_type").change(function(){
                var PurchasingOrder_invoice_type  = $(this).val();
                getInvoiceTypePercentage(PurchasingOrder_invoice_type);
            });
            
});', CClientScript::POS_HEAD);


$cs->registerScript("getContactCurrencyDetails", " function getContactCurrencyDetails(vendor_id) {
 var base_currency = " . Yii::app()->user->company_base_currency . ";
    jQuery.ajax({
        'dataType':'json', 
        'type': 'post',
        'url': '" . Yii::app()->createUrl('vendor/GetCurrencyWithAddress') . "',
        'data': {'vendor_id':vendor_id},
        'cache': false,
        'success': function(data) {
            $('#c_div').html(data.currency);
            $('#PurchasingOrder_exchange_rate').val(data.exchange_rate).addClass('filled');
            if(base_currency==$('#PurchasingOrder_currency').val()){
               $('#PurchasingOrder_exchange_rate').attr('readonly',true);
           }
           else{
               $('#PurchasingOrder_exchange_rate').attr('readonly',false);
           }
            // $('#PurchasingOrder_shipping_address').val(data.billaddress).addClass('filled');
            $('#PurchasingOrder_billing_address').val(data.billaddress).addClass('filled');
            $('#div_contact').html(data.contact); 
            $('#div_payment_term').html(data.payterm); 
        }
    });
    

}", CClientScript::POS_HEAD);
$cs->registerScript("getSupplierList", " function getSupplierList(agent_id) {
    jQuery.ajax({
        'dataType':'json', 
        'type': 'post',
        'url': '" . Yii::app()->createUrl('vendor/getvendorbytype') . "',
        'data': {'agent_id':agent_id},
        'cache': false,
        'success': function(data) {
            $('#supplier_div').html(data.vendorlist);
        }
    });

}", CClientScript::POS_HEAD);



$cs->registerScript("getExchangeRate", " function getExchangeRate(currency) {
            var base_currency = " . Yii::app()->user->company_base_currency . ";
            
            var currency_id = $('#'+currency).val();
                                jQuery.ajax({
                                        'type': 'post',
                                        'url': '" . Yii::app()->createUrl('lead/getExchangeRate') . "',
                                        'data': {'currency_id':currency_id},
                                        'cache': false,
                                        'success': function(data) {
                                           $('#PurchasingOrder_exchange_rate').val(data).addClass('filled');
                                           if(base_currency==currency_id){
                                               $('#PurchasingOrder_exchange_rate').attr('readonly',true);
                                           }
                                           else{
                                               $('#PurchasingOrder_exchange_rate').attr('readonly',false);
                                           }
                                        }
                                });

        }", CClientScript::POS_HEAD);
$cs->registerScript("putDateVal", " function putDateVal(date) {
                    var i=0;
                    for(i=0;i<50;i++){
                        try{
                            $('#PurchasingOrderStockItem_'+i+'_expected_delivery_date').val(date);
                        }catch(e){}
                    }
        }", CClientScript::POS_HEAD);
?>
<script>
    $(function () {
        var date_value = $("#PurchasingOrder_order_date").val().split("/");
        var current = new Date(date_value[2], date_value[1] - 1, date_value[0]);
        /*$('#PurchasingOrder_required_date').datetimepicker({  
            minDate:current,
            format: 'DD/MM/YYYY'
        });*/
        // var exp_date = $("#PurchasingOrder_required_date").val().split("/");
        // var current_2 = new Date(exp_date[2], exp_date[1] - 1, exp_date[0]);
        //  $('#PurchasingOrder_expected_delivery_date').datetimepicker({  
        //      minDate:current,
        //      maxDate:current_2,
        //      format: 'DD/MM/YYYY'
        //  });


        $("#PurchasingOrder_expected_delivery_date").on("dp.change", function (e) {
            var d = new Date(e.date);
            var duedate = new Date(d);
            duedate.setDate(d.getDate() + 0);
            var month = String(duedate.getMonth() + 1);
            var day = String(duedate.getDate());
            var year = String(duedate.getFullYear());

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            var final = `${day}/${month}/${year}`;
            //$('#Contract_contract_date_to').val(final);
            var i = 0;
            for (i = 0; i < 50; i++) {
                try {
                    $('#PurchasingOrderStockItem_' + i + '_expected_delivery_date').val(final);
                } catch (e) { }
            }

        });

        for (j = 0; j < 50; j++) {
            // $('#PurchasingOrderStockItem_'+j+'_expected_delivery_date').datetimepicker({  
            //      minDate:current,
            //      maxDate:current_2,
            //      format: 'DD/MM/YYYY'
            //  });


        }

    });
</script>