<?php
$this->load->view("partial/header");

$total_qty=0;

foreach ($cart as $cart_key => $cart_value) {
	$total_qty+=$cart_value['quantity'];
}

?>

<div id="page_title" style="margin-bottom:8px;"><?php echo $this->lang->line('sales_register'); ?>
    <span id="current_add">
        <?php
        //        if (end($cart)) {
        //            echo end($cart)["item_number"] . "@" . end($cart)["price"] . "&nbsp;BDT";
        //        }
        ?>
    </span>
</div>
<!--<div id="current_add" ><p>shad</p></div>-->
<?php
if (isset($error)) {
	echo "<div class='error_message'>" . $error . "</div>";
}

if (isset($warning)) {
	echo "<div class='warning_mesage'>" . $warning . "</div>";
}

if (isset($success)) {
	echo "<div class='success_message'>" . $success . "</div>";
}
?>
<div id="register_wrapper">

<?php echo form_open("sales/change_mode", array('id' => 'mode_form')); ?>

<table style="width:100">
	<tr>
		<td>
		<span><?php echo $this->lang->line('sales_mode') ?></span>
    <?php echo form_dropdown('mode', $modes, $mode, 'onchange="$(\'#mode_form\').submit();"id="change"'); ?>
		</td>
		<td style="padding-left:10px; width:64%;">
			<table width="100%">
				<tr>
					<td style="text-align:left; font-weight: bold; font-size:20px;" width="30%">
					Item: <?php echo $total_qty; ?>
					</td>
					<td style="text-align:right;font-weight: bold;font-size:20px;" width="100%">
					<?php if($total==0){?>
					Amount: <?php echo to_currency(0); ?>
					<?php }else{?>
					Amount: <?php echo to_currency(round($total + $previous_due-$purchase_discount_amount )); ?>
					<?php }?>
					</td>
				</tr>
		
			</table>
		</td>
		<td>
		<div id="show_suspended_sales_button">
        <?php
        echo anchor("sales/suspended/width:425", "<div class='small_button'><span style='font-size:73%;'>" . $this->lang->line('sales_suspended_sales') . "</span></div>", array('class' => 'thickbox none', 'title' => $this->lang->line('sales_suspended_sales')));
        ?>
    </div>
		</td>	
	</tr>

</table>



    
    
</form>
<?php echo form_open("sales/add", array('id' => 'add_item_form')); ?>
<label id="item_label" for="item">

    <?php
    if ($mode == 'sale' || $mode == 'global') {
    	echo $this->lang->line('sales_find_or_scan_item');
    } else {
    	echo $this->lang->line('sales_find_or_scan_item_or_receipt');
    }
    ?>
</label>
<?php
if ($mode == 'global') {
	echo form_input(array('name' => 'item', 'id' => 'find', 'size' => '65'));
} else if($mode == 'global_delivery'){
	echo form_input(array('name' => 'recept_id', 'id' => 'recept_id', 'size' => '65'));
}else {
	echo form_input(array('name' => 'item', 'id' => 'item', 'size' => '65'));
}
?>

<?php if ($mode == 'global'){?>
<div id="item_find" >
    <?php 
    //echo anchor("sales/globalsearch/-1/width:600", "<div class='small_button' id='find' ><span>" . $this->lang->line('sales_find') . "</span></div>", array('class' => 'thickbox none', 'id' => "button_find", 'title' => $this->lang->line('sales_find')));
    echo anchor("sales/globalsearch", "<div class='small_button' id='find' ><span>" . $this->lang->line('sales_find') . "</span></div>", array('class' => 'thickbox none', 'id' => "button_find", 'title' => $this->lang->line('sales_find')));
    ?>
</div>
<?php } else if($mode == 'global_delivery'){?>
<div id="item_find" >
<?php echo anchor("sales/global_delivery/-1/width:600", "<div class='small_button' id='recept_id' ><span>" . $this->lang->line('sales_find') . "</span></div>", array('class' => 'thickbox none', 'id' => "button_find", 'title' => $this->lang->line('sales_find'))); ?>
</div>    
<?php }?>

<?php //if ($mode != 'global') { ?>
    <!--<div id="new_item_button_register" >-->
        <?php
        //echo anchor("items/view/-1/width:360", "<div class='small_button'><span>" . $this->lang->line('sales_new_item') . "</span></div>", array('class' => 'thickbox none', 'title' => $this->lang->line('sales_new_item')));
        ?>
    <!--</div>-->
<?php //} else { ?>

    <!--<div id="item_find" >-->
        <?php
        //echo anchor("sales/globalsearch/-1/width:480", "<div class='small_button' id='find' ><span>" . $this->lang->line('sales_find') . "</span></div>", array('class' => 'thickbox none', 'id' => "button_find", 'title' => $this->lang->line('sales_find')));
        ?>
    <!--</div>-->
<?php //} ?>
<!--</form>-->
<?php
//echo '<pre>';
//print_r($cart);
//print_r($lot_details);
?>
<table id="register">
    <thead>
        <tr>
            <th style="width:11%;"><?php echo $this->lang->line('common_delete'); ?></th>
            <th style="width:10%;"><?php echo $this->lang->line('sales_item_number'); ?></th>
            <th style="width:30%;"><?php echo $this->lang->line('sales_item_name'); ?></th>
            <th style="width:11%;"><?php echo $this->lang->line('sales_price'); ?></th>
            <th style="width:11%;"><?php echo $this->lang->line('sales_quantity'); ?></th>
            <th style="width:11%;"><?php echo $this->lang->line('sales_discount'); ?></th>
            <th style="width:15%;"><?php echo $this->lang->line('sales_head_total'); ?></th>
            <?php if ($mode == 'global') { ?>
                <th style="width:15%;"><?php echo $this->lang->line('location'); ?></th>    
                <?php
            } else {
                ?>
                <th style="width:15%;"><?php echo $this->lang->line('sales_edit'); ?></th>  
                <?php
            }
            ?>
        </tr>
    </thead>
    <tbody id="cart_contents">
        <?php
        if (count($cart) == 0) {
            ?>
            <tr><td colspan='8'>
                    <div class='warning_message' style='padding:7px;'><?php echo $this->lang->line('sales_no_items_in_cart'); ?></div>
            </tr></tr>
            <?php
        } else {
        	 echo form_open("sales/edit_item_shawpon", array('id' => 'edit_item_shawpon_form')); ?></form>
        <?php

        foreach (array_reverse($cart, true) as $line => $item) {

        	// $cur_item_info = $this->Item->get_info_shawpon($item['item_id'],$item['lot_no']);
        	echo form_open("sales/edit_item/$line");
                ?>
                <tr>
                    <td><?php echo anchor_delete("sales/delete_item/$line", '[' . $this->lang->line('common_delete') . ']','',$item['item_id'],$item['name'],$item['lot_no']); ?></td>
                    <td><?php echo $item['item_id']."</br>L: ".$item['lot_no']; ?></td>
                    <td style="align:center;"><?php echo $item['name']; ?><br /> <?php echo ($mode != 'global') ? '[' . $item['stock_qty'] . ' in stock]' : '' ?></td>

                    <?php
                    if ($items_module_allowed) {
                        ?>
                        <td>
                            <?php //echo form_input(array('name' => 'price', 'value' => $item['price'], 'size' => '6')); ?>
                            <?php //echo number_format($item['price'], 2, '.', ''); ?>
                            <?php //echo form_hidden('price', $item['price']); ?>
                        </td>
                        <?php
                    } else {
                        ?>
                        <td>
                        
                        <?php 

                        $query = $this->db->query("
                    		SELECT * FROM stock_moves WHERE stock_id='".$item['item_id']."' AND lot_no='".$item['lot_no']."' AND loc_code='".LOCATION_CODE."' ORDER BY price DESC" );
                        $price_list=array();

                        foreach ($query->result() as $row) {
                        	$price_list[number_format($row->price, 2,".","")]=number_format($row->price, 2,".","");
                        }
                        if(count($price_list)>1){

                        ?>
                        
                         <select name="price" id="price" onchange="this.form.submit()">
                         
                      <?php foreach ($price_list as $_p_key => $p_value) { ?>
                      
                       <?php if(number_format($_p_key, 2,".","")==number_format($item['price'], 2,".","")){?>
                       <option value="<?php echo number_format($_p_key, 2,".","");?>" selected><?php echo number_format($_p_key, 2,".","");?></option>
                       <?php }else{?>
                      
                      	 <option value="<?php echo number_format($_p_key, 2,".","");?>"><?php echo number_format($_p_key, 2,".","");?></option>
                      	<?php }?>
                      	
                      <?php }?>  
                        
    </select>
                        <?php }else{?>
                        
                        
                        
                            <?php echo number_format($item['price'], 2, '.', ''); ?>
                            <?php echo form_hidden('price', $item['price']); ?>
                            
                            <?php }?>
                        </td>
                        <?php
                    }
                    ?>

                    <td>
                        <?php
                        //                        if ($item['is_serialized'] == 1) {
                        //                            echo $item['quantity'];
                        //                            echo form_hidden('quantity', $item['quantity']);
                        //                        } else {
                        //                            echo form_input(array('name' => 'quantity', 'value' => $item['quantity'], 'size' => '2'));
                        //                        }
                        if ($mode == 'global') {
                        	echo $item['quantity'];
                        	echo form_hidden('quantity', $item['quantity']);
                        } else {
                        	echo form_input(array('name' => 'quantity', 'value' => $item['quantity'], 'size' => '2'));
                        	// echo $item['quantity'];
                        	//echo form_hidden('quantity', $item['quantity']);
                        }
                        ?>
                    </td>

                    <td><?php 
                    if ($mode == 'global') {
                    	echo $item['discount'];
                    	echo form_hidden('discount', $item['discount']);
                    } else {
                    	//echo number_format($item['discount'], 2, '.', '');
                    	//echo form_hidden('discount', $item['discount']);
                    	echo form_input(array('name' => 'discount', 'value' => $item['discount'], 'size' => '3'));

                    }
                    	?>
                    </td>
                    <td><?php echo number_format(($item['price'] * $item['quantity']) - (($item['price'] * $item['quantity']) * ($item['discount'] / 100)), 2, '.', ''); ?></td>
                    <?php
                    if ($mode == 'global') {
                    	echo '<td>' . $item['location'] . ' </td>';
                    } else {
                        ?>

                        <td><?php echo form_submit("edit_item", $this->lang->line('sales_edit_item')); ?></td>
                    <?php } ?>

                </tr>
                <?php /*
                        <tr>
                        <td style="color:#2F4F4F";><?php echo $this->lang->line('sales_description_abbrv') . ':'; ?></td>
                        <td colspan=2 style="text-align:left;">

                        <?php
                        if ($item['allow_alt_description'] == 1) {
                        echo form_input(array('name' => 'description', 'value' => $item['description'], 'size' => '20'));
                        } else {
                        if ($item['description'] != '') {
                        echo $item['description'];
                        echo form_hidden('description', $item['description']);
                        } else {
                        echo 'None';
                        echo form_hidden('description', '');
                        }
                        }
                        ?>
                        </td>
                        <td>&nbsp;</td>
                        <td style="color:#2F4F4F";>
                        <?php
                        if ($item['is_serialized'] == 1) {
                        echo $this->lang->line('sales_serial') . ':';
                        }
                        ?>
                        </td>
                        <td colspan=3 style="text-align:left;">
                        <?php
                        if ($item['is_serialized'] == 1) {
                        echo form_input(array('name' => 'serialnumber', 'value' => $item['serialnumber'], 'size' => '20'));
                        } else {
                        echo form_hidden('serialnumber', '');
                        }
                        ?>
                        </td>


                        </tr>
                 */ ?>
                <tr style="height:3px">
                    <td colspan=8 style="background-color:white"> </td>
                </tr>		</form>
                <?php
        }
        }
        ?>
    </tbody>
</table>
</div>

<div id="overall_sale">
    <?php
    if (count($cart) > 0) {
    	if (trim($customer)) {
    		echo $this->lang->line("sales_customer") . ': <b>' . $customer . '</b><br />';

    		if(trim($customer_gs_card)!=""){
    			echo "Card No " . ': <b>' . $customer_gs_card . '</b><br />';
    		}
    		echo anchor("sales/remove_customer", '[' . $this->lang->line('common_remove') . ' ' . $this->lang->line('customers_customer') . ']');
        ?>
        <div style="margin-top:5px;text-align:left;">
            <?php if ($previous_due > 0) { ?>
                <font style="font-weight:bold;">Previous Due: </font><font style="font-weight:bold;color:#FF0000;"><?php echo to_currency($previous_due); ?></font>
            <?php } else if ($previous_due < 0) { ?>
                <font style="font-weight:bold;">Previous Advance: </font><font style="font-weight:bold;color:#078d20;"><?php echo to_currency(abs($previous_due)); ?></font>
            <?php } else { ?>
                <font style="font-weight:bold;">Previous Due: </font><font style="font-weight:bold;color:#000000;"><?php echo to_currency($previous_due); ?></font>
            <?php } ?>
        </div>
        <div style="margin-top:5px;text-align:left;">
            <font style="font-weight:bold;">This Year User Points:</font><font style="font-weight:bold;color:#078d20;"> <?php echo!empty($user_points) ? $user_points : 0 ?></font>
        </div>
        <div style="margin-top:5px;text-align:left;">
            <font style="font-weight:bold;">This Year Purchase:</font><font style="font-weight:bold;color:#078d20;"> <?php echo!empty($user_points) ? $user_points*100 : 0 ?> BDT</font>
        </div>
        <?php
    	} else {
    		echo form_open("sales/select_customer", array('id' => 'select_customer_form'));
        ?>
        <label id="customer_label" for="customer"><?php echo $this->lang->line('sales_select_customer'); ?></label>
        <?php echo form_input(array('name' => 'customer', 'id' => 'customer', 'size' => '30', 'value' => $this->lang->line('sales_start_typing_customer_name'))); ?>
        <?php
        echo anchor("customers/view/-1/width:350",
        "<div class='small_button' style='float: left;'><span>".$this->lang->line('customers_new')."</span></div>",
        array('class'=>'thickbox none','title'=>$this->lang->line('customers_new')));
        ?>
        
    </form>
    <div style="margin-top:5px;text-align:center;">
    <!--     <h3 style="margin: 5px 0 5px 0"><?php echo $this->lang->line('common_or'); ?></h3> -->
        <?php
        //     echo anchor("customers/view/-1/width:350", "<div class='small_button' style='margin:0 auto;'><span>" . $this->lang->line('sales_new_customer') . "</span></div>", array('class' => 'thickbox none', 'title' => $this->lang->line('sales_new_customer')));
        ?>
    </div>
    <div class="clearfix">&nbsp;</div>
    <?php
    	}
    }
?>

<div id='sale_details'>
    <div class="float_left" style="width:55%;"><?php echo $this->lang->line('sales_sub_total'); ?>:</div>
    <div class="float_left" style="width:45%;font-weight:bold;text-align:right;"><?php echo to_currency($subtotal); ?></div>
    <?php if ($this->session->userdata['customer'] != -1) { ?>
        <?php if ($previous_due >= 0) { ?>
            <div class="float_left" style="width:55%;"><?php echo "Previous Due"; ?>:</div>
        <?php } else { ?>
            <div class="float_left" style="width:55%;"><?php echo "Previous Advance"; ?>:</div>
        <?php } ?>

        <div class="float_left" style="width:45%;font-weight:bold;text-align:right;"><?php echo to_currency($previous_due); ?></div>

        <?php
    }
    $total_tax = 0.00;

    ?>
    <?php foreach ($taxes as $name => $value) { ?>
        <div class="float_left" style='width:55%;'><?php echo $name; ?>:</div>
        <div class="float_left" style="width:45%;font-weight:bold;text-align:right;"><?php echo to_currency($value); ?></div>
        <?php
        $total_tax = $total_tax + $value;
    };
    ?>

    <?php if ($this->session->userdata['customer'] != -1) { ?>
        <div class="float_left" style='width:70%;color:#FF0000;'>Use Points (<?php echo ($cart_point>0?$cart_point:0) . ' * ' . to_currency($cart_point_value) ?>) : </div>
        <div class="float_left" style="width:30%;font-weight:bold;text-align:right;color:#FF0000;"><?php echo "-" . to_currency($cart_point_cost); ?></div>
        
        <?php 	if(POINT_CARD_ACTIVE==1){ ?>
        <div class="float_left" style='width:70%;color:#FF0000;'>Purchase Discount (<?php echo $purchase_discount_percentage; ?> %) : </div>
        <div class="float_left" style="width:30%;font-weight:bold;text-align:right;color:#FF0000;"><?php echo "-" . to_currency($purchase_discount_amount); ?></div>
    <?php 
        }
        } ?>

    <?php

    $adjectment=0;
    $adjectment_show = number_format(round($total+ $previous_due+$total_tax-$purchase_discount_amount) - ($total + $total_tax+$previous_due-$purchase_discount_amount),2);

    if (trim($customer)=="") {
    	$purchase_discount_amount=0;
    }
    ?>
    <div class="float_left" style='width:55%;'>Adjustment(+/-):</div>
    <div class="float_left" style="width:45%;font-weight:bold;text-align:right;"><?php echo ($adjectment_show > 0 ? "+" . to_currency($adjectment_show) : to_currency($adjectment_show)); ?></div>
    <div class="float_left" style='width:55%;'><?php echo $this->lang->line('sales_total'); ?>:</div>
    <div class="float_left" style="width:45%;font-weight:bold;text-align:right;"><?php echo to_currency(round($total + $previous_due-$purchase_discount_amount )); ?></div>
</div>




<?php
// Only show this part if there are Items already in the sale.
if (count($cart) > 0) {
    ?>

    <div id="Cancel_sale">
        <?php echo form_open("sales/cancel_sale", array('id' => 'cancel_sale_form')); ?>
        <div class='small_button' id='cancel_sale_button' style='margin-top:5px;'>
            <span><?php echo $this->lang->line('sales_cancel_sale'); ?></span>
        </div>
        </form>
    </div>
    <div class="clearfix" style="margin-bottom:1px;">&nbsp;</div>
    <table style="border: 1px solid #000; width:100%">
            <?php
            $can_complete=0;
            if (isset($customer)) {
            	if(trim($customer_gs_card)==""){

            		if($purchase_discount_elegible_percentage_amount>=0){
            			echo form_open("sales/new_card", array('id' => 'select_new_card_form'));
            		?>
            		<tr>
		            	<th colspan="2" style="text-align:left; background-color: #b60b05;border: 0px solid #000; width:100%;color:#FFFFFF;vertical-align: middle;padding-top: 3px;height: 36px;">
            		<?php
            		echo "&nbsp;".form_input(array('name' => 'new_card_number', 'size' => '17')). '</b>';
            		?>
            		 	</th> 
            		 	<th  style="text-align:left; background-color: #b60b05;border: 0px solid #000;color:#FFFFFF;">
            		 	<?php
            		 	echo "<div class='small_button' id='add_new_card_button' style='float:left;margin-top:5px;'><span>Add Card</span></div>";
            		 	?>
            		 	</th>
	            	</tr>
	            	<?php
	            	echo form_hidden('purchase_discount_elegible_percentage_amount', $purchase_discount_elegible_percentage_amount);
	            	echo form_hidden('customer_gs_id', $customer_gs_id);
	            	echo form_hidden('customer_gs_card', $customer_gs_card);
	            	?>
	            	</form>
            	<?php
            	$can_complete=1;
            		}
            	}

            }
            ?>
    </table>
    </br>
     <?php

     if (doubleval($amount_due + $previous_due)<=0) {
     	if ($mode == 'global') {
     		$posLocations = [0 => $this->lang->line('select_locations'), LOCATION_CODE => LOCATION_NAME];
     		if ($this->session->userdata('global_receive_location_code') != LOCATION_CODE) {
     			$posLocations[$this->session->userdata('global_receive_location_code')] = $this->session->userdata('global_receive_location');
     		}
     	}
        ?>
        <div id="finish_sale">
           
            <table style="border: 1px solid #000; width:100%">
             <?php echo form_open("sales/complete", array('id' => 'finish_sale_form')); ?>
            <?php if ($mode == 'global') { ?>
                <label id="comment_label" for="comment" style="font-weight:bold;color:#2F38F2;"><?php echo $this->lang->line('common_locations'); ?>:</label>
                <?php echo form_dropdown('receive_location', $posLocations, '', 'id="receive_location"'); ?><br/><br/>
            <?php } 

            echo form_hidden('purchase_discount_elegible_percentage_amount', $purchase_discount_elegible_percentage_amount);
            echo form_hidden('customer_gs_id', $customer_gs_id);
            echo form_hidden('customer_gs_card', $customer_gs_card);

            ?>
            
                 
	            <tr>
		            <th colspan="3" style="text-align:center; background-color: #00ff00;border: 1px solid #000; width:100%">
		            BAG
		            </th> 
	            </tr>
             	<tr>
		            <th style="text-align:center; background-color: #00ff00;border: 1px solid #000; width:33%">
		            Big
		            </th>
		           <th style="text-align:center; background-color: #00ff00;border: 1px solid #000; width:33%">
		            Medium
		            </th>  
		            <th style="text-align:center; background-color: #00ff00;border: 1px solid #000; width:33%">
		            Small
		            </th> 
	            </tr>
            	<tr>
		            <th style="text-align:center; background-color: #008148;border: 1px solid #000; width:33%">
		            <?php echo form_input(array('name' => 'bag_big', 'value' => $bag_big, 'size' => '2'));?>
		            </th>
		           <th style="text-align:center; background-color: #008148;border: 1px solid #000; width:33%">
		            <?php echo form_input(array('name' => 'bag_medium', 'value' => $bag_medium, 'size' => '2'));?>
		            </th>  
		            <th style="text-align:center; background-color: #008148;border: 1px solid #000; width:33%">
		             <?php echo form_input(array('name' => 'bag_small', 'value' => $bag_small, 'size' => '2'));?>
		            </th> 
	            </tr>
            
            </table>
            
             <?php echo form_hidden('purchase_discount_amount', $purchase_discount_amount); ?>
            
            </br>
            
            <label id="comment_label" for="comment"><?php echo $this->lang->line('common_comments'); ?>:</label>
            <?php echo form_textarea(array('name' => 'comment', 'id' => 'comment', 'value' => $comment, 'rows' => '4', 'cols' => '23')); ?>
            <br /><br />

            <?php
            if (!empty($customer_email)) {
            	echo $this->lang->line('sales_email_receipt') . ': ' . form_checkbox(array(
            	'name' => 'email_receipt',
            	'id' => 'email_receipt',
            	'value' => '1',
            	'checked' => (boolean) $email_receipt,
            	)) . '<br />(' . $customer_email . ')<br />';
            }

            if ($payments_cover_total) {
            	if($can_complete==0){
            		echo "<div class='small_button' id='finish_sale_button' style='float:left;margin-top:5px;'><span>" . $this->lang->line('sales_complete_sale') . "</span></div>";
            	}
            }
            if ($mode != 'global') {
            	echo "<div class='small_button' id='suspend_sale_button' style='float:right;margin-top:5px;'><span>" . $this->lang->line('sales_suspend_sale') . "</span></div>";
            }
            ?>
        </div>
        </form>
        <?php
     }
    ?>



    <table width="100%">
        <tr>
            <td style="width:55%; "><div class="float_left"><?php echo 'Payments Total:' ?></div></td>
            <td style="width:45%; text-align:right;"><div class="float_left" style="text-align:right;font-weight:bold;"><?php echo to_currency($payments_total); ?></div></td>
        </tr>
        <?php if (!empty($cart_point)) { ?>
            <tr>
                <td style="width:55%; "><div class="float_left"><?php echo 'Use Points:' ?></div></td>
                <td style="width:45%; text-align:right;"><div class="float_left" style="text-align:right;font-weight:bold;"><?php echo "(".$cart_point. '*'.$cart_point_value."=" . to_currency($cart_point*$cart_point_value).")" ?></div></td>
            </tr>
        <?php } 


        ?>
        <tr>

            <td style="width:55%; "><div class="float_left" ><?php echo 'Trans. Amount Due:' ?></div></td>
            <?php if ($amount_due + $previous_due < 0) { ?>
                <td style="width:45%; text-align:right; "><div class="float_left" style="text-align:right;font-weight:bold;color:#008000;font-size: 150%; "><?php echo to_currency(abs($amount_due + $previous_due)); ?></div></td>
            <?php } elseif($amount_due + $previous_due ==0) { ?>
                <td style="width:45%; text-align:right; "><div class="float_left" style="text-align:right;font-weight:bold;color:#F4FF41;font-size: 150%; "><?php echo to_currency(abs($amount_due + $previous_due)); ?></div></td>
            <?php } else { ?>
                <td style="width:45%; text-align:right; "><div class="float_left" style="text-align:right;font-weight:bold;color:#FF3A3A;font-size: 150%; "><?php echo to_currency(abs($amount_due + $previous_due)); ?></div></td>
            <?php } ?>
        </tr>
    </table>

    <div id="Payment_Types" >

        <div style="height:auto;">

            <?php echo form_open("sales/add_payment", array('id' => 'add_payment_form')); ?>
            <table width="100%">
                <tr>
                    <td>
                        <?php echo $this->lang->line('sales_payment') . ':   '; ?>
                    </td>
                    <td>
                        <?php echo form_dropdown('payment_type', $payment_options, array(), 'id="payment_types"'); ?>
                    </td>
                </tr>
                <tr id="hide-row">
                    <td>
                        <?php echo $this->lang->line('sales_amount_tendered') . ': '; ?></span>
                    </td>
                    <td>
                        <?php 

                        echo form_input(array('name' => 'amount_tendered', 'id' => 'amount_tendered', 'value' => to_currency_no_money($amount_due + $previous_due), 'size' => '10')); ?>
                    </td>
                </tr>
            </table>
            <?php
            // add div which depend(Change or Hide) on dropdown value
            //-------------------------------------start----------------------
            ?>

            
            <div id="cod" style="display: none">
                <table width="100%">

                    <tr>
                        <td><?php echo 'Transaction ID' . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'transaction_payment_id_cod', 'id' => 'transaction_payment_id_cod', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                </table>

            </div> 
            <div id="ssl" style="display: none">
                <table width="100%">

                    <tr>
                        <td><?php echo 'Transaction ID' . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'transaction_payment_id_ssl', 'id' => 'transaction_payment_id_ssl', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                </table>

            </div>  
            <div id="bkash" style="display: none">
                <table width="100%">

                   <!-- <tr>
                        <td style="color:#FF0000;"><?php //echo 'BKash Discount (10%)' . ': '; ?></td>
                        <td><?php //echo form_input(array('name' => 'bkash_discount', 'id' => 'bkash_discount', 'value' => '', 'size' => '10','readonly'=>'true')); ?></td>
                    </tr>--> 
                    <tr>
                        <td><?php echo 'Phone No' . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'transaction_phone_no_bkash', 'id' => 'transaction_phone_no_bkash', 'value' => '', 'size' => '10')); ?></td>
                    </tr> 
                    <tr>
                        <td><?php echo 'Transaction ID' . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'transaction_payment_id_bkash', 'id' => 'transaction_payment_id_bkash', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                </table>

            </div> 

            <div id="check" style="display: none">
                <table width="100%">

                    <tr>
                        <td><?php echo $this->lang->line('check_no') . ': '; ?></td>

                        <td><?php echo form_input(array('name' => 'check_name', 'id' => 'check_name', 'value' => '', 'size' => '20')); ?></td>
                    </tr>
                    <tr>
                        <td> <?php echo '<label>' . $this->lang->line('ac_no') . ': ' . '</label>'; ?></td>

                        <td> <?php echo form_input(array('name' => 'ac_no_check', 'id' => 'ac_no_check', 'value' => '', 'size' => '20')); ?></td>
                    </tr>
                    <tr>
                        <td>  <?php echo $this->lang->line('holder_name') . ': '; ?></td>

                        <td>  <?php echo form_input(array('name' => 'holder_name_check', 'id' => 'holder_name_check', 'value' => '', 'size' => '20')); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('bank_name') . ':'; ?></td>
                        <td> <?php echo form_dropdown('bank_name_check', $bank_id, array(), 'id="bank_name_check"'); ?></td>
                    </tr>
                    <tr>
                        <td> <?php echo $this->lang->line('branch_name') . ': '; ?></td>
                        <td>                <?php echo form_input(array('name' => 'branch_name_check', 'id' => 'branch_name_check', 'value' => '', 'size' => '20')); ?>
                        </td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('check_date') . ': '; ?> </td>
                        <td><?php echo form_input(array('name' => 'check_date', 'id' => 'check_date', 'value' => '', 'size' => '20')); ?>
                        </td>
                    </tr>
                </table>

            </div>

            <div id="debit" style="display: none">
                <table>
                    <tr>
                        <td><?php echo $this->lang->line('bank_name') . ':'; ?></td>
                        <td><?php echo form_dropdown('bank_name_debit', $bank_id, array(), 'id="bank_name"'); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('card_no') . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'card_no_debit', 'id' => 'card_no', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('holder_name') . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'holder_name_debit', 'id' => 'holder_name', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                </table>

            </div>



            <div id="credit" style="display: none">
                <table>
                    <tr>
                        <td><?php echo $this->lang->line('bank_name') . ':'; ?></td>
                        <td><?php echo form_dropdown('bank_name_credit', $bank_id, array(), 'id="bank_name"'); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('card_no') . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'card_no_credit', 'id' => 'card_no', 'value' => '', 'size' => '10')); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('holder_name') . ': '; ?></td>
                        <td><?php echo form_input(array('name' => 'holder_name_credit', 'id' => 'holder_name', 'value' => '', 'size' => '10')); ?></td>
                    </tr>

                    <tr>
                        <td><?php echo $this->lang->line('card_expire_month') . ':'; ?></td>
                        <td><?php echo form_dropdown('card_expire_month', $month, array(), 'id="card_expire_month"'); ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $this->lang->line('card_expire_year') . ':'; ?></td>
                        <td><?php echo form_dropdown('card_expire_year', $year, array(), 'id="card_expire_year"'); ?> </td>
                    </tr>

                </table>

            </div>

            <div id="points" style="display: none">
                <table>
                    <tr>
                        <td>
                            <?php echo $this->lang->line('use_point') . ': '; ?></span>
                        </td>
                        <td>
                            <?php echo form_input(array('name' => 'user_point', 'id' => 'user_point', 'value' => '', 'size' => '10')); ?>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="due" style="display: none">
               
            </div>

            <?php
            if(($amount_due + $previous_due)>0){

            ?>
            <div class='small_button' id='add_payment_button' style='float:left;margin-top:5px;'>
                <span><?php echo $this->lang->line('sales_add_payment'); ?></span>
            </div>
<?php }?>
        </div>
        </form>
    </div>
    <?php
    // Only show this part if there is at least one payment entered.
    if (count($payments) > 0 || !empty($cart_point)) {
        ?>
        <table id="register">
            <thead>
                <tr>
                    <th style="width:11%;"><?php echo $this->lang->line('common_delete'); ?></th>
                    <th style="width:60%;"><?php echo 'Type'; ?></th>
                    <th style="width:18%;"><?php echo 'Amount'; ?></th>


                </tr>
            </thead>
            <tbody id="payment_contents">
                <?php
                foreach ($payments as $payment_id => $payment) {
                	if ($payment['payment_type'] != 'User Point') {
						
						$payment_type_show=$payment['payment_type'];
                		if($payment['payment_type']=="Purchase Discount"){
                			$payment_type_show="Sales Discount";
                		}elseif($payment['payment_type']=="Purchase Bonus"){
                			$payment_type_show="Special Sales Discount";
                		}
                		echo form_open("sales/edit_payment/$payment_id", array('id' => 'edit_payment_form' . $payment_id));
                        ?>
                        <tr>
                            <td><?php echo anchor("sales/delete_payment/$payment_id", '[' . $this->lang->line('common_delete') . ']'); ?></td>
                            <td><?php echo $payment_type_show; ?></td>
                            <td style="text-align:right;"><?php echo to_currency($payment['payment_amount']); ?></td>
                        </tr>
                        </form>
                        <?php
                	}
                }
                ?>
                <?php
                if (!empty($cart_point)) {
                    ?>
                    <tr>
                        <td><?php echo anchor("sales/delete_point", '[' . $this->lang->line('common_delete') . ']'); ?></td>
                        <td>Point (<?php echo $cart_point ?>)</td>
                        <td style="text-align:right;"><?php echo '-' . to_currency($cart_point_cost); ?></td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
        <br />
        <?php
    }
}
?>
</div>
<div class="clearfix" style="margin-bottom:30px;">&nbsp;</div>


<?php $this->load->view("partial/footer"); ?>

<script type="text/javascript" language="javascript">
$(document).ready(function ()
{
	$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	$("#find").blur(function () {
		var find_val = $(this).val();

		$("#button_find").attr('href', '<?php echo base_url(); ?>index.php/sales/globalsearch/' + find_val + '/width:480');
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$("#recept_id").blur(function () {
		var find_val = $(this).val();

		$("#button_find").attr('href', '<?php echo base_url(); ?>index.php/sales/global_delivery/' + find_val + '/width:480');
		$(this).attr('placeholder', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$('#item').bind('keyup', function() {

		var itemLength = $("#item").val().length;
		if(itemLength==14){
			if($("#item").val().match(/^\d+$/)) {
				$('#add_item_form').submit();
			}
		}
	});

	$("#item").autocomplete('<?php echo site_url("sales/item_search"); ?>',
	{
		minChars: 0,
		max: 100,
		selectFirst: false,
		delay: 10,
		formatItem: function (row) {
			//return row[1];
			return (row.length > 1 && row[1]) || row[0];
		}
	});


	$("#item").result(function (event, data, formatted)
	{
		$("#add_item_form").submit();
	});
	//
	//        var modechange = $("#change").val();
	//     $("#mode_form").change(function() {
	//
	//            var mode = $("#change").val();
	//
	//              alert(modechange);
	//                    $.ajax({
	//                            type: "POST",
	//                            url: '<?php echo site_url("sales/change_mode"); ?>',
	//                            data: { change: modechange },
	//                            success:function(data){
	//                                alert(data);
	//                            }
	//
	//                            });
	//
	//
	//                });
	//





	$('#item').focus();
	$('#find').focus();
	$('#recept_id').focus();

	$('#item').blur(function ()
	{
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_item_name'); ?>");
	});

	$("#find").autocomplete('<?php echo site_url("sales/item_search"); ?>',
	{
		minChars: 0,
		max: 100,
		selectFirst: false,
		delay: 10,
		formatItem: function (row) {
			return row[1];
		}
	});

	$('#check_date').datePicker();


	$('#item,#customer,#find,#recept_id').click(function ()
	{
		$(this).attr('value', '');

	});

	$("#customer").autocomplete('<?php echo site_url("sales/customer_search"); ?>',
	{
		minChars: 0,
		delay: 10,
		max: 100,
		formatItem: function (row) {
			return row[1];
		}
	});

	$("#customer").result(function (event, data, formatted)
	{
		$("#select_customer_form").submit();
	});

	$('#customer').blur(function ()
	{
		$(this).attr('value', "<?php echo $this->lang->line('sales_start_typing_customer_name'); ?>");
	});

	$('#comment').change(function ()
	{
		$.post('<?php echo site_url("sales/set_comment"); ?>', {comment: $('#comment').val()});
	});

	$('#receive_location').change(function ()
	{
		$.post('<?php echo site_url("sales/set_receive_location"); ?>', {receive_location: $('#receive_location').val()});
	});

	$('#email_receipt').change(function ()
	{
		$.post('<?php echo site_url("sales/set_email_receipt"); ?>', {email_receipt: $('#email_receipt').is(':checked') ? '1' : '0'});
	});


	$("#finish_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_finish_sale"); ?>'))
		{
			$('#finish_sale_form').submit();
		}
	});
	$("#add_new_card_button").click(function ()
	{
		if (confirm('<?php echo "Are you sure you want to add this card? This cannot be undone."; ?>'))
		{
			$('#select_new_card_form').submit();
		}
	});

	$("#suspend_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_suspend_sale"); ?>'))
		{
			$('#finish_sale_form').attr('action', '<?php echo site_url("sales/suspend"); ?>');
			$('#finish_sale_form').submit();
		}
	});

	$("#cancel_sale_button").click(function ()
	{
		if (confirm('<?php echo $this->lang->line("sales_confirm_cancel_sale"); ?>'))
		{
			$('#cancel_sale_form').submit();
		}
	});

	$("#edit_item_shawpon_button").click(function ()
	{
		$('#edit_item_shawpon_form').submit();
	});

	$("#add_payment_button").click(function ()
	{
		$('#add_payment_form').submit();
	});



	$("#payment_types").change(checkPaymentTypeGiftcard).ready(checkPaymentTypeGiftcard)
});

function post_item_form_submit(response)
{
	if (response.success)
	{
		$("#item").attr("value", response.item_id);
		$("#add_item_form").submit();
	}
}

function post_person_form_submit(response)
{
	if (response.success)
	{
		$("#customer").attr("value", response.person_id);
		$("#select_customer_form").submit();
	}
}
function checkPaymentTypeGiftcard()
{
	if ($("#payment_types").val() == "<?php echo $this->lang->line('sales_giftcard'); ?>")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_giftcard_number'); ?>");
		$("#amount_tendered").val('');
		$("#amount_tendered").focus();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#cash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "<?php echo $this->lang->line('sales_cash'); ?>")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cash").show();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "Purchase Bonus")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cash").show();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "COD Pathao")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cod").show();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#cash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	}
	else if ($("#payment_types").val() == "SSL COMMERZ")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cod").hide();
		$("#ssl").show();
		$("#bkash").hide();
		$("#cash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	}

	else if ($("#payment_types").val() == "BKash")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
			$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").show();
		$("#cash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	}


	else if ($("#payment_types").val() == "Due")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#cash").hide();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#check").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").show();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "<?php echo $this->lang->line('sales_check'); ?>")
	{

		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#check").show();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#cash").hide();
		$("#debit").hide();
		$("#credit").hide();
		$("#gift").hide();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "<?php echo $this->lang->line('sales_debit'); ?>")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#credit").hide();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#gift").hide();
		$("#check").hide();
		$("#cash").hide();
		$("#debit").show();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "<?php echo $this->lang->line('sales_credit'); ?>")
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
		$("#amount_tendered").val('<?php echo to_currency_no_money($amount_due + $previous_due + $adjectment); ?>');
		$("#check").hide();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#cash").hide();
		$("#debit").hide();
		$("#gift").hide();
		$("#credit").show();
		$("#points").hide();
		$("#due").hide();
		$('#hide-row').show();
		$('#amount_tendered').show();
	} else if ($("#payment_types").val() == "User Point") {
		$('#hide-row').hide();
		$("#amount_tendered").val('');
		$("#amount_tendered").hide();
		$("#check").hide();
		$("#cod").hide();
		$("#ssl").hide();
		$("#bkash").hide();
		$("#cash").hide();
		$("#debit").hide();
		$("#gift").hide();
		$("#credit").hide();
		$("#due").hide();
		$("#points").show();
	} else
	{
		$("#amount_tendered_label").html("<?php echo $this->lang->line('sales_amount_tendered'); ?>");
	}
}



//$("#add_payment_button").click(function(){
//
//       $.ajax({
//                    type: 'POST',
//                    data: $('#add_payment_form').serialize(),
//                    url: 'index.php/sales/add_payment',
//                    success: function (resp) {
//                        if (resp) {
//                           alert('hi');
//                        }
//
//                    }
//                });
//
//
//    });
//







</script>