
<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;}
            #sales-man-recovery-report-search-form{display: none;}
            .print_show{display: block !important;}
            .print_hide{display: none !important;}
            table{width: 100% !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_sales_man_receovery_rep_breadcrumb_title'); ?></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_sales_man_receovery_rep_breadcrumb_title'); ?></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_sales_man_receovery_rep_title'); ?></h2>
                    <form id="sales-man-recovery-report-search-form" action="<?= url('/sales-man-receovery'); ?>">
                    <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_sales_man_receovery_rep_filter_currency'); ?></label>
                                        <select class="form-control chosen-select" name="currency">
                                            <?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 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sales_man_receovery_rep_filter_sales_man_marketer'); ?></label>
                                        <select class="form-control chosen-select" name="sales_man">
                                            <option value=""><?= print_lang($app_lang,'mod_sales_man_receovery_rep_filter_sales_man_marketer'); ?></option>
                                            <?php foreach($marketers as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($search_sales_man == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= print_lang($app_lang,'mod_sales_man_receovery_rep_filter_sales_man_text_marketer'); ?>)</option>
                                            <?php } ?>
                                            <?php foreach($sales_man as $row){ ?>
                                                <option value="<?= $row['uid']; ?>" <?php if($search_sales_man == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?> (<?= print_lang($app_lang,'mod_sales_man_receovery_rep_filter_sales_man_text_sales_man'); ?>)</option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-3 pd2">
                                    <div class="form-group">
                                        <label><?= print_lang($app_lang,'mod_sales_man_receovery_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_sales_man_receovery_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>

                    <table class="table table-bordered bg-white" style="width: 100%;">
                        <thead>
                            <tr>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_currency'); ?></th>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= array_key_exists($search_currency, $currencies) ? $currencies[$search_currency]['name'] : NULL; ?></th>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_from'); ?></th>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= $search_from; ?></th>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_to'); ?></th>
                                <th style="background: #f4f4f4 !important;" colspan="2"><?= $search_to; ?></th>
                            </tr>
                            <tr>
                                <th style="background: #f4f4f4 !important;" class="text-center"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_sr_num'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_client'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_mobile'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_old_balance'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_sales'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_journal_debits'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_return'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_net_balance'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_receipts'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_journal_credit'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_pending'); ?></th>
                                <th style="background: #f4f4f4 !important;"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_recovery'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $i = 1; 
                            $old_balance_totals = [];
                            $sales_totals = [];
                            $journal_debits_totals = [];
                            $sales_return_totals = [];
                            $net_balance_totals = [];
                            $money_receipts_totals = [];
                            $journal_credits_totals = [];
                            $pending_totals = [];
                            foreach($report_amounts as $row){

                                $old_balance_debit = (float)$row['opening_debit'];
                                $old_balance_credit = (float)$row['opening_credit'];
                                $old_balance = $old_balance_debit - $old_balance_credit;
                                $sales = (float)$row['sales'];
                                $journal_debits = (float)$row['journal_debits'];
                                $sales_return = (float)$row['sales_return'];
                                $net_balance = $old_balance + $sales + $journal_debits - $sales_return;
                                $journal_credits_db = (float)$row['journal_credits'];
                                $money_receipts = isset($receipts_amounts[$row['uid']]) ? $receipts_amounts[$row['uid']]['amount'] : 0;
                                $journal_credits = $journal_credits_db - $money_receipts;
                                $pending = $net_balance - $journal_credits_db;
                                if($net_balance == 0)
                                {
                                    $percentage = 0;
                                }
                                else
                                {
                                    $percentage = ($journal_credits_db * 100) / $net_balance;
                                }
                            ?>
                                <tr>                                        
                                    <td class="text-center"><?= $i++; ?></td>
                                    <td><?= $row['name']; ?></td>
                                    <td><?= $row['mobile']; ?></td>
                                    <td><?= amount_number_format_0($old_balance); $old_balance_totals[] = $old_balance; ?></td>
                                    <td><?= amount_number_format_0($sales); $sales_totals[] = $sales; ?></td>
                                    <td><?= amount_number_format_0($journal_debits); $journal_debits_totals[] = $journal_debits; ?></td>
                                    <td><?= amount_number_format_0($sales_return); $sales_return_totals[] = $sales_return; ?></td>
                                    <td><?= amount_number_format_0($net_balance); $net_balance_totals[] = $net_balance; ?></td>
                                    <td><?= amount_number_format_0($money_receipts); $money_receipts_totals[] = $money_receipts; ?></td>
                                    <td><?= amount_number_format_0($journal_credits); $journal_credits_totals[] = $journal_credits; ?></td>
                                    <td><?= amount_number_format_0($pending); $pending_totals[] = $pending; ?></td>
                                    <td><?= amount_number_format_0($percentage).'%'; ?></td>
                                </tr>
                            <?php } ?>
                            <?php
                                if(array_sum($net_balance_totals) == 0)
                                {
                                    $total_percentage = 0;
                                }
                                else
                                {
                                    $total_received = array_sum($journal_credits_totals) + array_sum($money_receipts_totals);
                                    $total_percentage = ($total_received * 100) / array_sum($net_balance_totals);
                                }
                            ?>
                                <tr>
                                    <td class="text-center"><?= $i++; ?></td>
                                    <td colspan="2"><?= print_lang($app_lang,'mod_sales_man_receovery_rep_thead_totals'); ?></td>
                                    <td><?= amount_number_format_0(array_sum($old_balance_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($sales_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($journal_debits_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($sales_return_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($net_balance_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($money_receipts_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($journal_credits_totals)); ?></td>
                                    <td><?= amount_number_format_0(array_sum($pending_totals)); ?></td>
                                    <td><?= amount_number_format_0($total_percentage).'%'; ?></td>
                                </tr>
                        </tbody>
                    </table>

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