
<?php

    if($userData['type']!=USER_TYPE_MPO){
        $users = $db->selectAll('users','where type='.USER_TYPE_MPO.' and isActive=1','id,name');
        $base = $db->selectAll('base');
    }
    $categoryData=$db->getCategoryData();
    $productData=$db->getProductData('and type in('.PRODUCT_TYPE_FINISHED.','.PRODUCT_TYPE_GIFT_ITEM.')');
    //$customers = $db->selectAll('customer','where isActive=1','id,name,code,due_day');
    $q=[];
    $q[]='isActive =1';
    $q[]='base_id !=0';
    if($userData['type']==USER_TYPE_MPO){
        $q[]  ='base_id='.  $userData['base_id'];
    }
    $sq='where '.implode(' and ',$q);

    $customers=$db->selectAll('customer',$sq.' order by name asc','id,name,code,due_day,base_id');


    $general->arrayIndexChange($customers,'id');
    if(!empty($customers)){
        foreach($customers as $k=>$p){
            $customers[$k]['name']=$p['code'].' '.$p['name'];
        }
    }
    $purDate=date('d-m-Y');
    $use_product_category = $db->get_company_settings('use_product_category');
    $products=[];
    if($use_product_category==0){
        $products = $db->selectAll('products','where type in('.PRODUCT_TYPE_FINISHED.','.PRODUCT_TYPE_GIFT_ITEM.') and isActive=1','id as pID, title,code');
        if(!empty($products)){
            foreach($products as $k=>$p){
                $products[$k]['title']=$p['code'].' '.$p['title'];
            }
        }
    }
    if($userData['type']!=USER_TYPE_MPO){
        $mpo_customers = [];
        foreach($customers as $c){
            $mpo_customers[$c['base_id']][]=$c;
        }
    }
$customer_id=0;
if(isset($_GET['customer-id'])){
    $customer_id=intval($_GET['customer-id']);
}
?>
<script type="text/javascript">
    <?php
        if($userData['type']!=USER_TYPE_MPO){
            echo 'var mpo_customers='.json_encode($mpo_customers).';';
        }

    ?>
    <?php echo 'var USE_PRODUCT_CATEGORY='.$use_product_category.';';?>
    <?php echo 'var productData='.json_encode($productData).';';?>
    <?php echo 'var categoryData='.json_encode($categoryData).';';?>
    <?php echo 'var USER_TYPE='.$userData['type'].';';?>
    <?php echo 'var customers='.json_encode($customers).';';?>
    function get_customer_due_day(customer_id){  
        console.log('customer_id',customer_id);
        $('#due_day').val(parse_int(customers[customer_id].due_day))
    }
    $(document).on('change','#cID',function(){get_customer_due_day(this.value)});
    function mpo_wise_customer(base_id){
        $('#cID').html('<option value="">Select customer</option>');
        if(base_id>0){
            if(typeof(mpo_customers[base_id])!='undefined'){
                $.each(mpo_customers[base_id],function(a,b){

                    $('#cID').append('<option value="'+b.id+'">'+b.name+'</option>');
                });
                select2Call();
            }
        }

    }
    $(document).on('change','#base_id',function(){mpo_wise_customer(this.value)});
    function sale_customer_change(customer_id){

        $('#customer_input_area .select2-container').attr('style','background-color:#FFFFFF');

        $.ajax({
            type:'post',
            url:ajUrl,
            data:{  module:'customerCurrentBalance',cID:customer_id},
            success:function(data){
                console.log('data',data)
                if(data.status==1){
                    if(data.can_invoice==0){
                        $('#customer_input_area .select2-container').attr('style','background-color:red');
                    }
                }
                //swMessageFromJs(data.m);
            }
        }); 

    }
</script>
<section class="mb-4" id="sale_area">
    <div class="card-header text-center py-3"> 
        <h5 class="mb-0 text-center">
            <strong>New order</strong>
        </h5>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-10">
                    <?php
                        if($userData['type']!=USER_TYPE_MPO){
                            $general->inputBoxSelect($base,'Base','base_id','id','title');   
                            $customers=[] ;
                        }                  

                    ?>  
                    <?php //$general->inputBoxSelect($customers,'Customer','cID','id','name',@$_GET['customer-id'],haveSelect:'Select customer');?> 
                    <div id="customer_input_area"><?php $general->inputBoxSelect($customers,'Customer','cID','id',columnTitle: 'name',currentValue: $customer_id,script:'onchange="sale_customer_change(this.value)"');?></div>

                    <?php $general->inputBoxText('due_day','Due day');?>
                </div>
                <div class="col-2">
                    <a href="<?=URL.'/app/?'.MODULE_URL.'=customer&add=1&form-order'?>" class="btn btn-info">Customer Add</a>
                </div>
            </div>
            <hr/> 
            <?php $general->inputBoxText('saleDate','Date',$purDate,'','daterangepicker');?>
            <?php $general->inputBoxSelect([['id'=>PAY_TYPE_CREDIT,'title'=>'Credit'],['id'=>PAY_TYPE_CASH,'title'=>'Cash']],'Pay type','pay-type','id','title','');?>
            <?php
                if($use_product_category==1){
                ?>
                <?php $general->inputBoxSelect($categoryData,'Category','category','id','title',haveSelect:'Select category');?>

                <?php $general->inputBoxSelect([],'Sub Category','subCategory','id','title'); ?>
                <?php
                }
            ?>
            <?php $general->inputBoxSelect($products,'Product','pID','pID','title',script:'onchange="saleProductChange(this.value)"');?>
            <?php $general->inputBoxText('product-tp','TP','','','amount_td','readonly');?>
            <?php $general->inputBoxText('productSalePrice','Sale Price','','','amount_td');?>
            <div class="form-group row">
                <label for="qty" class="col-md-4 col-form-label">Quantity</label>
                <div class="col-md-8">
                    <div class="input-group qty-input-group">
                        <input class="form-control amount_td" value="" placeholder="Quantity" id="productSaleqty" type="text">
                        <div class="input-group-append">
                            <span class="input-group-text" id="qtyLabel">--</span>
                        </div>
                    </div>
                </div>
            </div>
            <?php $general->inputBoxText('freeQTY','Free QTY','','','amount_td');?>
            <?php $general->inputBoxText('product-discount','Discount','','','amount_td','readonly');?>
            <?php $general->inputBoxText('total','Total','','','amount_td','readonly');?>
            <hr/>
            <div>
                <button class="form-control btn btn-info" onclick="productSaleAddToCart()">Add to cart</button>
            </div>
            <div style="display: none;">
                <table>
                    <tbody>
                        <tr id="saleProductsTr">
                            <td class="autoSerial"></td>
                            <td>
                                <input type="hidden" class="pID" value="">
                                <span class="pTitle"></span>
                            </td>
                            <?php
                                if($use_product_category==1){
                                ?>
                                <td class="category"></td>
                                <td><input type="hidden" class="categoryID" value=""><span class="subCategory"></span></td>
                                <?php
                                }
                            ?>
                            <td class="unTitle"></td>
                            <td class="salePrice-tp amount_td"></td>
                            <td class="salePrice amount_td"></td>
                            <td class="qty amount_td"></td>
                            <td class="free_qty amount_td"></td>
                            <td class="total_qty amount_td"></td>
                            <td class="sub_total amount_td"></td>
                            <td class="discunt amount_td"></td>
                            <td class="total amount_td"></td>
                            <td class="amount_td"><button class="btn btn-danger remove">X</button></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="table-responsive">
                <table class="table table-striped" id="reportTable">
                    <thead>
                        <tr>
                            <th  style="width: 2%;">#</th>
                            <th>Product</th>
                            <?php
                                if($use_product_category==1){
                                ?>
                                <th>Category</th>
                                <th>Sub Category</th>
                                <?php
                                }
                            ?>
                            <th >Unit</th>
                            <th  class="amount_td ">TP</th>
                            <th  class="amount_td">Sale Price</th>
                            <th class="amount_td">Qty</th>
                            <th  class="amount_td">Free Qty</th>
                            <th  class="amount_td">Total Qty</th>
                            <th style="width: 10%;" class="amount_td">Sub total</th>
                            <th style="width: 10%;" class="amount_td">Discount</th>
                            <th style="width: 10%;" class="amount_td">Total</th>
                            <th style="width: 3%;" class="amount_td">X</th>
                        </tr>
                    </thead>
                    <tbody id="saleProducts">

                    </tbody>

                    <tfoot>
                        <?php
                            $col_minus = 0;
                            if($use_product_category==0){
                                $col_minus = 2;
                            }


                        ?>
                        <tr>
                        <tr>
                            <td colspan="<?=11-$col_minus?>" class="amount_td"><b>Total</b></td>
                            <td class="amount_td"><b id="total_product_discount"></b></td>
                            <td class="amount_td"><b id="subTotal"></b></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="<?=12-$col_minus?>" class="amount_td"><b>Discount</b></td>
                            <td class="amount_td"><input type="text" class="form-control amount_td" value="" id="saleDiscount"></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="<?=12-$col_minus?>" class="amount_td"><b>Net Payable</b></td>
                            <td class="amount_td"><b id="netPayable"></b></td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td colspan="<?=11-$col_minus?>" class="amount_td"></td>
                            <td colspan="<?=4-$col_minus?>"><?php $general->inputBoxTextArea('note','Note') ?></td>
                            <td>&nbsp;</td>
                        </tr>


                        <tr>
                            <td colspan="<?=13-$col_minus?>" class="amount_td">
                                <button  onclick="productsSaleDraft()" class="mt-1 btn-lg btn-success pull-right productsSaleDraft">Submit order</button>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>

        </div>
    </div>
</section>
