<?php $item_settings = get_item_settings(); $currency_data = fetch_table_where('currency','uid',$search_currency,true); ?>

<style>
    ::-webkit-scrollbar {
        display: none;
    }
    #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;}
        body {
        -webkit-print-color-adjust:exact;
        background: #fff;
        }
</style>

<table style="width: 100%; background: #fff;">
    <tr>
        <td><img style="width: 100%; height: 200px;" src="<?= get_branch_image('report'); ?>"></td>
    </tr>
</table>



<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,'expiry_report_title'); ?></h2>

                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_sr'); ?></th>
                                    <th class="text-center" style="width:150px;"><?= print_lang($app_lang,'expiry_report_thead_name'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_category'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_currency'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_unit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_avg_cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_last_cost'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_sale_price'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_stock_in'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_stock_out'); ?></th>
                                    <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_bti'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_bto'); ?></th>
                                    <?php } ?>
                                    <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_tin'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_tout'); ?></th>
                                    <?php } ?>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_damage'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_ava'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_value'); ?></th>

                                    <?php if($item_settings['item_measurements'] == 'get'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'width'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'height'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'length'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'cube_meter'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'total_cube_meter'); ?></th>
                                    <?php } ?>

                                    <th class="text-center"><?= print_lang($app_lang,'expiry_report_thead_image'); ?></th>
                                    <th class="text-center" style="min-width:60px;"><?= print_lang($app_lang,'expiry_report_thead_expiry_date'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $i = 1;
                                $total_avg_cost = array();
                                $total_last_cost = array();
                                $total_first_cost = array();
                                $total_sales_price = array();
                                $total_stock_in_units = array();
                                $total_stock_out_units = array();
                                $total_branch_transfer_in_units = array();
                                $total_branch_transfer_out_units = array();
                                $total_ph_transfer_in_units = array();
                                $total_ph_transfer_out_units = array();
                                $total_stock_damage_units = array();
                                $total_stock_avail_units = array();
                                $total_stock_avail_units_value = array();
                                $total_of_item_width = array();
                                $total_of_item_height = array();
                                $total_of_item_length = array();
                                $total_of_item_cube_meter = array();
                                $total_of_item_total_cube_meter = array();
                                foreach($pageData as $row){
                                    $ava_qty = sum_up_array_values($row['stats']['avail_units']);
                                    if($ava_qty > 0){
                                ?>
                                    <tr class="text-center">
                                        <td><?= $i++; ?></td>
                                        <td><?= $row['name']; ?></td>
                                        <td><?= array_key_exists($row['category'], $product_categories) ? $product_categories[$row['category']]['name'] : NULL; ?></td>

                                        <?php
                                        $currency_name = array_key_exists($row['currency'], $currencies) ? $currencies[$row['currency']]['name'] : NULL;
                                        if(!empty($search_currency))
                                        {
                                            $currency_name = array_key_exists($search_currency, $currencies) ? $currencies[$search_currency]['name'] : NULL;
                                        }
                                        ?>

                                        <td><?= $currency_name; ?></td>
                                        
                                        <td>
                                            <?php if($row['purchase_unit'] == $search_unit){ ?>
                                                <span class="text-info font-bold"><?= array_key_exists($row['purchase_unit'], $units) ? $units[$row['purchase_unit']]['short'] : NULL; ?></span>
                                            <?php }else if($row['sale_unit'] == $search_unit){ ?>
                                                <span class="text-info font-bold"><?= array_key_exists($row['sale_unit'], $units) ? $units[$row['sale_unit']]['short'] : NULL; ?></span>
                                            <?php }else if($row['production_unit'] == $search_unit){ ?>
                                                <span class="text-info font-bold"><?= array_key_exists($row['production_unit'], $units) ? $units[$row['production_unit']]['short'] : NULL; ?></span>
                                            <?php }else{ ?>
                                                <span class="text-info font-bold"><?= array_key_exists($row['purchase_unit'], $units) ? $units[$row['purchase_unit']]['short'] : NULL; ?></span>
                                            <?php } ?>
                                        </td>

                                        <?php

                                        // currency conversion
                                        $item_avg_cost = $row['stats']['avg_cost'];
                                        $item_last_cost = $row['stats']['last_cost'];
                                        $item_first_cost = $row['cost'];
                                        $item_sale_price = $row['stats']['avg_cost'] + $row['stats']['last_profit'];

                                        $item_stock_in_units = sum_up_array_values($row['stats']['stock_in_units']);
                                        $item_stock_out_units = sum_up_array_values($row['stats']['stock_out_units']);

                                        $item_branch_transfer_in_units = sum_up_array_values($row['stats']['branch_transfer_in_units']);
                                        $item_branch_transfer_out_units = sum_up_array_values($row['stats']['branch_transfer_out_units']);
                                        
                                        $item_ph_transfer_in_units = sum_up_array_values($row['stats']['transfer_in_pharma']);
                                        $item_ph_transfer_out_units = sum_up_array_values($row['stats']['transfer_out_pharma']);

                                        $item_damage_units = sum_up_array_values($row['stats']['damage_units']);
                                        $item_avail_units = sum_up_array_values($row['stats']['avail_units']);

                                        if(!empty($search_currency))
                                        {
                                            $item_avg_cost = amount_conversion($item_avg_cost, $row['currency_usd_equalance'], $currency_data['usd_equalance']);
                                            $item_last_cost = amount_conversion($item_last_cost, $row['currency_usd_equalance'], $currency_data['usd_equalance']);
                                            $item_first_cost = amount_conversion($item_first_cost, $row['currency_usd_equalance'], $currency_data['usd_equalance']);
                                            $item_sale_price = amount_conversion($item_sale_price, $row['currency_usd_equalance'], $currency_data['usd_equalance']);
                                        }

                                        if(!empty($search_unit))
                                        {
                                            if($row['purchase_unit'] == $search_unit){ $unit_identifier = 'purchase'; }
                                            else if($row['sale_unit'] == $search_unit){ $unit_identifier = 'sale'; }
                                            else if($row['production_unit'] == $search_unit){ $unit_identifier = 'production'; }
                                            else{ $unit_identifier = 'purchase'; }

                                            $item_avg_cost = amount_unit_based_conversion($row,$unit_identifier,$item_avg_cost);
                                            $item_last_cost = amount_unit_based_conversion($row,$unit_identifier,$item_last_cost);
                                            $item_first_cost = amount_unit_based_conversion($row,$unit_identifier,$item_first_cost);
                                            $item_sale_price = amount_unit_based_conversion($row,$unit_identifier,$item_sale_price);

                                            $item_stock_in_units = unit_based_conversion($row,$unit_identifier,$item_stock_in_units);
                                            $item_stock_out_units = unit_based_conversion($row,$unit_identifier,$item_stock_out_units);
                                            $item_branch_transfer_in_units = unit_based_conversion($row,$unit_identifier,$item_branch_transfer_in_units);
                                            $item_branch_transfer_out_units = unit_based_conversion($row,$unit_identifier,$item_branch_transfer_out_units);
                                            $item_ph_transfer_in_units = unit_based_conversion($row,$unit_identifier,$item_ph_transfer_in_units);
                                            $item_ph_transfer_out_units = unit_based_conversion($row,$unit_identifier,$item_ph_transfer_out_units);
                                            $item_damage_units = unit_based_conversion($row,$unit_identifier,$item_damage_units);
                                            $item_avail_units = unit_based_conversion($row,$unit_identifier,$item_avail_units);
                                        }

                                        $item_width = (float)$row['width'];
                                        $item_height = (float)$row['height'];
                                        $item_length = (float)$row['length'];
                                        $item_cube_meter = $item_width * $item_height;
                                        $item_total_cube_meter = $item_cube_meter * $item_avail_units;

                                        ?>

                                        <td><?= round_decimels_digits($item_avg_cost); $total_avg_cost[] = $item_avg_cost; ?></td>
                                        <td><?= round_decimels_digits($item_last_cost); $total_last_cost[] = $item_last_cost; ?></td>
                                        <td><?= round_decimels_digits($item_sale_price); $total_sales_price[] = $item_sale_price; ?></td>
                                        
                                        <td><?= round_decimels_digits($item_stock_in_units); $total_stock_in_units[] = $item_stock_in_units; ?></td>
                                        <td><?= round_decimels_digits($item_stock_out_units); $total_stock_out_units[] = $item_stock_out_units; ?></td>
                                        <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                            <td><?= round_decimels_digits($item_branch_transfer_in_units); $total_branch_transfer_in_units[] = $item_branch_transfer_in_units; ?></td>
                                            <td><?= round_decimels_digits($item_branch_transfer_out_units); $total_branch_transfer_out_units[] = $item_branch_transfer_out_units; ?></td>
                                        <?php } ?>
                                        <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                            <td><?= round_decimels_digits($item_ph_transfer_in_units); $total_ph_transfer_in_units[] = $item_ph_transfer_in_units; ?></td>
                                            <td><?= round_decimels_digits($item_ph_transfer_out_units); $total_ph_transfer_out_units[] = $item_ph_transfer_out_units; ?></td>
                                        <?php } ?>
                                        <td><?= round_decimels_digits($item_damage_units); $total_stock_damage_units[] = $item_damage_units; ?></td>
                                        <td><?= round_decimels_digits($item_avail_units); $total_stock_avail_units[] = $item_avail_units; ?></td>
                                        <td><?= round_decimels_digits($item_avail_units * $item_avg_cost); $total_stock_avail_units_value[] = $item_avail_units * $item_avg_cost; ?></td>
                                        
                                        <?php if($item_settings['item_measurements'] == 'get'){ ?>
                                            <td><?= round_decimels_digits( $item_width ); $total_of_item_width[] = $item_width; ?></td>
                                            <td><?= round_decimels_digits( $item_height ); $total_of_item_height[] = $item_height; ?></td>
                                            <td><?= round_decimels_digits( $item_length ); $total_of_item_length[] = $item_length; ?></td>
                                            <td><?= round_decimels_digits( $item_cube_meter ); $total_of_item_cube_meter[] = $item_cube_meter; ?></td>
                                            <td><?= round_decimels_digits( $item_total_cube_meter ); $total_of_item_total_cube_meter[] = $item_total_cube_meter; ?></td>
                                        <?php } ?>

                                        <td>
                                            <?php
                                            $item_image = base_path().'/uploads/items/'.$row['item_image'];
                                            if(is_file($item_image))
                                            {
                                                $item_image = url('/').'/uploads/items/'.$row['item_image'];
                                            ?>
                                            <?php if(check_user_access('view_item_image') === TRUE){ ?>
                                                <a href="<?= $item_image; ?>" target="_blank" class="btn btn-xs btn-outline btn-primary url-elem"><i class="fa fa-file-photo-o">&nbsp; </i><?= print_lang($app_lang,'image'); ?></a>
                                            <?php } ?>
                                            <?php
                                            }
                                            ?>
                                        </td>

                                        <td><?= display_true_date($row['expiry_date']); ?></td>
                                    </tr>
                                <?php }} ?>

                                <tr class="text-center">
                                    <td></td>
                                    <td><strong><?= print_lang($app_lang,'total'); ?></strong></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_avg_cost)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_last_cost)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_sales_price)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_stock_in_units)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_stock_out_units)); ?></strong></td>
                                    <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                        <td><strong><?= round_decimels_digits(array_sum($total_branch_transfer_in_units)); ?></strong></td>
                                        <td><strong><?= round_decimels_digits(array_sum($total_branch_transfer_out_units)); ?></strong></td>
                                    <?php } ?>

                                    <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                        <td><strong><?= round_decimels_digits(array_sum($total_ph_transfer_in_units)); ?></strong></td>
                                        <td><strong><?= round_decimels_digits(array_sum($total_ph_transfer_out_units)); ?></strong></td>
                                    <?php } ?>
                                    <td><strong><?= round_decimels_digits(array_sum($total_stock_damage_units)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_stock_avail_units)); ?></strong></td>
                                    <td><strong><?= round_decimels_digits(array_sum($total_stock_avail_units_value)); ?></strong></td>

                                    <?php if($item_settings['item_measurements'] == 'get'){ ?>
                                        <td><strong><?= round_decimels_digits( array_sum($total_of_item_width) ); ?></strong></td>
                                        <td><strong><?= round_decimels_digits( array_sum($total_of_item_height) ); ?></strong></td>
                                        <td><strong><?= round_decimels_digits( array_sum($total_of_item_length) ); ?></strong></td>
                                        <td><strong><?= round_decimels_digits( array_sum($total_of_item_cube_meter) ); ?></strong></td>
                                        <td><strong><?= round_decimels_digits( array_sum($total_of_item_total_cube_meter) ); ?></strong></td>
                                    <?php } ?>

                                    <td></td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>    
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>