
<style type="text/css">
    .print_show{display: none !important;}
    @media print {
            #dynamic-content-container{background: #fff;}
            .navbar-default.navbar-static-side{display: none !important;}
            #page-wrapper{margin: 0px !important; padding: 0 !important;}
            .row.border-bottom{display: none !important;}
            .footer.fixed{display: none;}
            .ibox-content{padding: 0 !important;}
            #sale-and-return-report-search-form{display: none;}
            .print_show{display: block !important;}
            .print_hide{display: none !important;}
            body {
            -webkit-print-color-adjust:exact;
            background: #fff;
            ::-webkit-scrollbar {
                display: none;
            }
        }
    }

</style>
<?php $item_settings = get_item_settings(); ?>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2><?= print_lang($app_lang,'mod_sale_and_return_rep_breadcrumb'); ?></h2>
        <ol class="breadcrumb">
            <li>
                <a class="url-elem" href="<?php echo url('/dashboard'); ?>"><?= print_lang($app_lang,'home'); ?></a>
            </li>
            <li>
                <a class="url-elem" href="<?php echo url('/reports'); ?>"><?= print_lang($app_lang,'reports'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'mod_sale_and_return_rep_breadcrumb'); ?></strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">
        
    </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">
                    <h2 class="text-center font-bold" style="margin: 10px 0 20px;"><?= print_lang($app_lang,'mod_sale_and_return_rep_title'); ?></h2>
                    <form id="sale-and-return-report-search-form" action="<?= url('/reports/sales-and-return'); ?>">
                    <div class="row">
                        <div class="col-md-9">
                            <div class="row">
                                <div class="col-md-3 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sale_and_return_rep_filter_currency'); ?></label>
                                        <select class="form-control chosen-select" name="currency">
                                            <?php $currency_data = array(); foreach($currencies as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($search_currency == $row['uid']){ $currency_data = $row; echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= $row['symbol']; ?>)</option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-3 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sale_and_return_rep_filter_client'); ?></label>
                                        <select class="form-control chosen-select" name="client">
                                            <?php foreach($clients as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($search_client == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> </option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sale_and_return_rep_filter_from'); ?></label>
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" value="<?= $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="<?= $search_from; ?>">
                                        <?php } ?>
                                    </div>
                                </div>
                                <div class="col-md-3 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sale_and_return_rep_filter_to'); ?></label>
                                        <?php if($branch_calander == 'persian'){ ?>
                                            <input type="text" class="form-control persian-date" value="<?= $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="<?= $search_to; ?>">
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2 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>
                        </div>
                        <div class="col-md-1" style="margin-top: 23px;">
                            <a href="javascript:void(0)" onclick="window.print()" class="btn btn-outline btn-warning dim"><i class="fa fa-print"></i></a>
                        </div>
                    </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-bordered bg-white">
                            <thead>
                                <tr>
                                    <th style="background: #f4f4f4 !important;" colspan="2"><?= print_lang($app_lang,'mod_sale_and_return_rep_info_currency'); ?></th>
                                    <th style="background: #f4f4f4 !important;"><?= array_key_exists($search_currency, $currencies) ? $currencies[$search_currency]['name'] : NULL; ?></th>
                                    <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sale_and_return_rep_info_from_date'); ?></th>
                                    <th style="background: #f4f4f4 !important;"><?= $search_from; ?></th>
                                    <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sale_and_return_rep_info_to_date'); ?></th>
                                    <th style="background: #f4f4f4 !important;"><?= $search_to; ?></th>
                                </tr>
                                <tr>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_sr'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_date'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_type'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_invoice'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_client'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_sales'); ?></th>
                                    <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_return'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 1; 
                                $sales_total = [];
                                $return_total = [];
                                foreach($pageData as $row){
                                    $row_type = isset($row['sale_id']) ? 'Return' : 'Invoice';
                                    $row_invoice = isset($row['sale_id']) ? $row['return_invoice'] : $row['invoice'];
                                    $edit_module = isset($row['sale_id']) ? 'sales-return' : 'sales';
                                    $sales_amount = isset($row['sale_id']) ? NULL : (float)$row['total_sales_amount'];
                                    $return_amount = isset($row['sale_id']) ? (float)$row['refund_total'] : NULL;

                                    $invoice_url = url('/'.$edit_module.'/invoice/'.$row['uid']);
                                ?>
                                    <tr class="text-center">                                        
                                        <td><?= $i++; ?></td>
                                        <td><?= display_true_date(explode(' ',$row['created_at'])[0]); ?></td>
                                        <td><?= $row_type; ?></td>
                                        <td>
                                            <a target="_blank" class="print_hide" href="<?= $invoice_url; ?>"><?= $row_invoice; ?></a>
                                            <a class="print_show"><?= $row_invoice; ?></a>
                                        </td>
                                        <td><?= array_key_exists($row['client'], $clients) ? $clients[$row['client']]['name'] : NULL; ?></td>
                                        <td><?= amount_number_format($sales_amount); $sales_total[] = $sales_amount; ?></td>
                                        <td><?= amount_number_format($return_amount); $return_total[] = $return_amount; ?></td>
                                    </tr>
                                <?php } ?>
                                    <tr class="text-center">                                        
                                        <td colspan="5" class="text-center" style="font-size: 15px;"><?= print_lang($app_lang,'mod_sale_and_return_rep_total'); ?></td>
                                        <td style="font-size: 15px;"><?= amount_number_format(array_sum($sales_total)); ?></td>
                                        <td style="font-size: 15px;"><?= amount_number_format(array_sum($return_total)); ?></td>
                                    </tr>
                                    <tr class="text-center">                                        
                                        <td style="font-size: 15px;" colspan="5" class="text-center"><?= print_lang($app_lang,'mod_sale_and_return_rep_net_sales'); ?></td>
                                        <td style="font-size: 15px;" colspan="2"><?= amount_number_format(array_sum($sales_total) - array_sum($return_total)); ?></td>
                                    </tr>
                            </tbody>
                        </table>    
                    </div>

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