
<?php if($live_search_data == 'no'){ ?>

<style>
    .pagination {
        margin: 0 !important;
    }
</style>

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-8">
        <h2><?= print_lang($app_lang,'sales'); ?></h2>
        <ol class="breadcrumb">
            <li>
                <a class="url-elem" href="<?php echo url('/dashboard'); ?>"><?= print_lang($app_lang,'home'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'sales'); ?></strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-4">
        <div class="title-action">
            <?php if(check_user_access('add_sale') === TRUE){ ?>
                <a href="<?= url('/sales/add'); ?>" class="btn btn-primary btn-sm custom-font-btn"><i class="fa fa-plus"></i>&nbsp; <?= print_lang($app_lang,'add_new_sale'); ?></a>
            <?php } ?>
        </div>
    </div>
</div>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">

                    <!-- spinner -->
                    <div class="sk-spinner sk-spinner-three-bounce">
                        <div class="sk-bounce1"></div>
                        <div class="sk-bounce2"></div>
                        <div class="sk-bounce3"></div>
                    </div>
                    <!-- spinner -->

                    <h2 class="text-center font-bold" style="margin: 10px 0 20px;"><?= print_lang($app_lang,'sales_listing'); ?></h2>

                    <form id="sales-search-form" action="<?= url('/sales'); ?>">
                    <div class="row">

                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'search_keyword'); ?></label>
                                <input type="text" class="form-control" name="_search" value="<?= $_search; ?>">
                            </div>
                        </div>

                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'currency'); ?></label>
                                <select class="form-control chosen-select" name="currency">
                                    <option value="" selected="">-- <?= print_lang($app_lang,'currency'); ?> --</option>
                                    <?php foreach($currencies as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_currency == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= $row['symbol']; ?>)</option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'convert_currency'); ?></label>
                                <select class="form-control chosen-select" name="convert_currency">
                                    <option value="" selected="">-- <?= print_lang($app_lang,'convert_currency'); ?> --</option>
                                    <?php foreach($currencies as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_convert_currency == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= $row['symbol']; ?>)</option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'account'); ?></label>
                                <select class="form-control chosen-select" name="client">
                                    <option value="" selected="">-- <?= print_lang($app_lang,'supplier_client'); ?> --</option>
                                    <?php foreach($clients as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_client == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name'].' ( Client)'; ?></option>
                                    <?php } ?>
                                    <?php foreach($suppliers as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_client == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name'].' ( Supplier)'; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'marketer'); ?></label>
                                <select class="form-control chosen-select" name="marketer">
                                    <option value="" selected="">-- <?= print_lang($app_lang,'marketer'); ?> --</option>
                                    <?php foreach($marketers as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_marketer == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']." ".$row['last_name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'from'); ?></label>
                                <?php if($branch_calander == 'persian'){ ?>
                                    <input type="text" class="form-control persian-date" value="<?= true_date_for_inputs($search_from); ?>" name="from" placeholder="<?= print_lang($app_lang,'from'); ?>" readonly="" style="background: #fff;">
                                <?php }else{ ?>
                                    <input type="date" class="form-control" name="from" value="<?= true_date_for_inputs($search_from); ?>">
                                <?php } ?>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'to'); ?></label>
                                <?php if($branch_calander == 'persian'){ ?>
                                    <input type="text" class="form-control persian-date" value="<?= true_date_for_inputs($search_to); ?>" name="to" placeholder="<?= print_lang($app_lang,'to'); ?>" readonly="" style="background: #fff;">
                                <?php }else{ ?>
                                    <input type="date" class="form-control" name="to" value="<?= true_date_for_inputs($search_to); ?>">
                                <?php } ?>
                            </div>
                        </div>

                        <div class="col-md-2">
                            <div class="form-group">
                                <label><?= print_lang($app_lang,'user'); ?></label>
                                <select class="form-control chosen-select" name="user">
                                    <option value="" selected="">-- <?= print_lang($app_lang,'user'); ?> --</option>
                                    <?php foreach($users as $row){ ?>
                                        <option value="<?= $row['uid']; ?>" <?php if($search_user == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-4 text-right" style="margin-top: 23px;">
                            <button class="btn btn-w-m btn-primary custom-font-btn-search" type="submit"><?= print_lang($app_lang,'search'); ?></button>
                            <a href="#" data-date="<?= display_true_date(date('Y-m-d')); ?>" class="today-date-form-trigger btn btn-primary custom-font-btn-search"><?= print_lang($app_lang,'today'); ?></a>
                            <a href="<?= url('/sales-all'); ?>" target="_blank" class="btn btn-primary custom-font-btn-search"><?= print_lang($app_lang,'all'); ?></a>
                        </div>
                    </div>
                    </form>
                    <hr>

                    <div class="table-responsive">

                        <!-- custom live search -->
                        <div class="custom-search-input" style="position: relative; top: 30px; z-index: 1; display: none;">
                            <input style="width: 200px; height: 30px;" type="text" title="Search" placeholder="Search" name="_hp_live_search" value="<?= $_search; ?>" class="form-control" data-params="currency,client,from,to" data-action="<?= url('/sales'); ?>">
                        </div>
                        <!-- custom live search -->

                        <table class="table table-bordered table-hover dataTables-view">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'date'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'invoice_#'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'client'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'currency'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'sales_total_amount'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'discount'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'invoice_total'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'received'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'remaining'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'transport_expenses'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'profit'); ?></th>
                                    
                                    <th class="text-center"><?= print_lang($app_lang,'memo'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'by'); ?></th>
                                    <th class="text-center" style="min-width: 120px; max-width:120px;"><?= print_lang($app_lang,'action'); ?></th>
                                </tr>
                            </thead>
                            <tbody id="page-records-body">
<?php } ?>
                                


                                <?php
                                $total_sales_amount = array();
                                $total_sales_amount_overall = array();
                                $total_received_amount = array();
                                $total_remaining_amount = array();
                                $total_trans_exp_amount = array();
                                $total_profit_amount = array();
                                $total_discount_amount = array();
                                ?>
                                <?php $i = 1; foreach($pageData as $row){

                                        $row_sub_total = $row['sub_total'];
                                        $row_invoice_total = $row['total_sales_amount'];
                                        $row_cash_received = $row['cash_received'];
                                        $row_remaining_amount = $row['remaining_amount'];
                                        $row_trans_expense = $row['trans_expense'];
                                        $row_total_profit_amount = $row['total_profit_amount'];
                                        $row_total_discount_amount = $row['total_discount_amount'];

                                        $currency_show = array_key_exists($row['currency'], $currencies) ? $currencies[$row['currency']]['name'] : NULL;
                                        
                                        if(!empty($search_convert_currency))
                                        {
                                            $convert_currency_record = fetch_table_where('currency','uid',$search_convert_currency,true);
                                            $currency_show = $convert_currency_record['name'];
                                            $amount_currency_equalance = $row['currency_usd_equalance'];
                                            $convert_currency_equalance = $convert_currency_record['usd_equalance'];
                                            if($row['currency'] == $search_convert_currency)
                                            {
                                                $amount_currency_equalance = 1;
                                                $convert_currency_equalance = 1;
                                            }
                                            
                                            $row_sub_total = amount_conversion($row['sub_total'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_invoice_total = amount_conversion($row['total_sales_amount'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_cash_received = amount_conversion($row['cash_received'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_remaining_amount = amount_conversion($row['remaining_amount'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_trans_expense = amount_conversion($row['trans_expense'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_total_profit_amount = amount_conversion($row['total_profit_amount'], $amount_currency_equalance, $convert_currency_equalance);
                                            $row_total_discount_amount = amount_conversion($row['total_discount_amount'], $amount_currency_equalance, $convert_currency_equalance);
                                            
                                        }

                                        $total_sales_amount[] = $row_sub_total;
                                        $total_sales_amount_overall[] = $row_invoice_total;
                                        $total_received_amount[] = $row_cash_received;
                                        $total_remaining_amount[] = $row_remaining_amount;
                                        $total_trans_exp_amount[] = $row_trans_expense;
                                        $total_profit_amount[] = $row_total_profit_amount;
                                        $total_discount_amount[] = $row_total_discount_amount;

                                ?>
                                    <tr class="text-center">
                                        <td><?= $i++; ?></td>
                                        <td><?= display_true_date(explode(' ',$row['created_at'])[0]); ?></td>
                                        <td><?= $row['invoice']; ?></td>
                                        <td>
                                            <?php 
                                            if(strpos($row['client'],"CASHSALE") === FALSE){
                                                echo array_key_exists($row['client'], $clients) ? $clients[$row['client']]['name'] : NULL; 
                                            }
                                            else
                                            {
                                                echo $row['cash_client_name'];
                                            }
                                            ?>
                                            <?= array_key_exists($row['client'], $suppliers) ? $suppliers[$row['client']]['name'] : NULL; ?>
                                        </td>
                                        <td><?= $currency_show; ?></td>
                                        <td><?= round_decimels_digits($row_invoice_total); ?></td>
                                        <td><?= round_decimels_digits($row_total_discount_amount); ?></td>
                                        <td><?= round_decimels_digits($row_sub_total); ?></td>
                                        <td><?= round_decimels_digits($row_cash_received); ?></td>
                                        <td><?= round_decimels_digits($row_remaining_amount); ?></td>
                                        <td><?= round_decimels_digits($row_trans_expense); ?></td>
                                        <td><?= round_decimels_digits($row_total_profit_amount); ?></td>
                                       
                                        <td><?= $row['memo']; ?></td>
                                        <td><?= $row['user_name']; ?></td>
                                        <td>
                                            <?php if($row['main_approval'] == 'approved'){?>
                                                <?php if(check_user_access('add_sale') === TRUE){ ?>
                                                    <a href="<?= url('/sales/deliver-note').'/'.$row['uid']; ?>" target="_blank" class="btn btn-xs btn-outline btn-success url-elem"><i class="fa fa-truck"></i></a>
                                                <?php } ?>
                                                <?php if(check_user_access('add_sale') === TRUE){ ?>
                                                    <a href="<?= url('/sales/invoice').'/'.$row['uid']; ?>" target="_blank" class="btn btn-xs btn-outline btn-warning url-elem"><i class="fa fa-external-link"></i></a>
                                                <?php } ?>
                                                <?php if(check_user_access('edit_sale') === TRUE){ ?>
                                                    <a href="<?= url('/sales/edit').'/'.$row['uid']; ?>" class="btn btn-xs btn-outline btn-primary url-elem"><i class="fa fa-pencil"></i></a>
                                                <?php } ?>
                                                <?php if(check_user_access('delete_sale') === TRUE){ ?>
                                                    <a href="<?= url('/sales/delete').'/'.$row['uid']; ?>" class="btn btn-xs btn-outline btn-danger delete-btn-trigger"><i class="fa fa-trash"></i></a>
                                                <?php } ?>

                                            <?php }else{ ?>
                                                <a href="javascript:void(0);" class="btn btn-xs btn-outline btn-warning" data-msg="You will not be able to revert this" data-mood="Delete"><i class="fa fa-eye"></i>&nbsp; <?= print_lang($app_lang,'pending'); ?></a>
                                            <?php } ?>
                                        </td>
                                    </tr>
                                <?php } ?>
                                <tr class="text-center">
                                    <td><?= $i++; ?></td>
                                    <td><?= display_true_date(date('Y-m-d')); ?></td>
                                    <td></td>
                                    <td></td>
                                    <td><?= print_lang($app_lang,'total'); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_sales_amount_overall)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_discount_amount)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_sales_amount)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_received_amount)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_remaining_amount)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_trans_exp_amount)); ?></td>
                                    <td><?= round_decimels_digits(array_sum($total_profit_amount)); ?></td>
                                    
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                </tr>


<?php if($live_search_data == 'no'){ ?>
                            </tbody>
                        </table>    
                    </div>

                    <div class="pagination-wrap">
                        <div class="row">
                            <div class="col-md-6" id="pagination-stats">
                                <span class="records-stats">Showing <?= (float)$records['from']; ?> - <?= (float)$records['to']; ?> of <?= $records['total']; ?></span>
                            </div>
                            <div class="col-md-6 text-right" id="pagination-pages">
                                <?= $pagination; ?>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<?php } ?>