<?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,'warehouse_details'); ?></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('/warehouse'); ?>"><?= print_lang($app_lang,'warehouse'); ?></a>
            </li>
            <li class="active">
                <strong><?= print_lang($app_lang,'details'); ?></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;"><?= $warehouse_data['name']; ?> <?= print_lang($app_lang,'details'); ?></h2>
                    
                    <form id="warehouse-details-search-form" action="<?= url('/warehouse/details').'/'.$warehouse_data['uid']; ?>">
                    <div class="row">
                        <div class="col-md-9">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <div class="input-group date">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            <select class="form-control chosen-select" name="category">
                                                <option selected="" value="">-- <?= print_lang($app_lang,'select_category'); ?> --</option>
                                                <?php foreach($product_categories as $row){ ?>
                                                    <option value="<?= $row['uid']; ?>" <?php if($search_category == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <div class="input-group date">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            <select class="form-control chosen-select" name="currency">
                                                <option selected="" value="">-- <?= print_lang($app_lang,'select_currency'); ?> --</option>
                                                <?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']; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <div class="input-group date">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            <select class="form-control chosen-select" name="unit">
                                                <option selected="" value="">-- <?= print_lang($app_lang,'select_unit'); ?> --</option>
                                                <?php foreach($units as $row){ ?>
                                                    <option value="<?= $row['uid']; ?>" <?php if($search_unit == $row['uid']){ echo 'selected="selected"'; } ?>><?= $row['name']; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-2 text-right">
                            <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">
                            <a target="_blank" href="<?= url('/warehouse/details-print').'/'.$warehouse_data['uid'].'?'.'&category='.$search_category.'&currency='.$search_currency.'&unit='.$search_unit.''; ?>" class="btn btn-outline btn-primary dim"><i class="fa fa-print"></i></a>
                        </div>
                    </div>
                    </form>

                    <hr>

                    <div class="table-responsive">
                        <table class="table table-bordered table-hover dataTables-view">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width: 27px !important; min-width: 27px !important;"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center" style="width: 200px; min-width: 200px;"><?= print_lang($app_lang,'name'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'category'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'item_country_thead'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'item_company_thead'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'item_details_thead'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'currency'); ?></th>
                                    <th class="text-center" style="min-width: 70px;"><?= print_lang($app_lang,'unit'); ?></th>
                                    <th class="text-center" style="width: 60px; min-width: 60px;"><?= print_lang($app_lang,'avg_cost'); ?></th>
                                    <th class="text-center" style="width: 60px; min-width: 60px;"><?= print_lang($app_lang,'last_cost'); ?></th>
                                    <th class="text-center" style="width: 70px; min-width: 70px;"><?= print_lang($app_lang,'sale_price'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'stock_in'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'stock_out'); ?></th>
                                    <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'branch_transfer_in_short'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'branch_transfer_out_short'); ?></th>
                                    <?php } ?>

                                    <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'ph_transfer_in_short'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'ph_transfer_out_short'); ?></th>
                                    <?php } ?>

                                    <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                                        <th class="text-center"><?= print_lang($app_lang,'bundle_ava'); ?></th>
                                        <th class="text-center"><?= print_lang($app_lang,'toop_ava'); ?></th>
                                    <?php } ?>
                                    <th class="text-center"><?= print_lang($app_lang,'damage'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'available'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'value'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'image'); ?></th>
                                    <th class="text-center" style="width:120px;"><?= print_lang($app_lang,'transfer'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $i = 1; 
                                $w_purchases_totals = [];
                                $w_sales_totals = [];
                                $w_tin_totals = [];
                                $w_tout_totalst = [];
                                $w_tout_totals = [];
                                $w_btin_totals = [];
                                $w_btout_totals = [];
                                $w_btout_totalst = [];
                                $w_damage_totals = [];
                                $w_bundle_totals = [];
                                $w_toop_totals = [];
                                $w_ava_totals = [];
                                $w_value_totals = [];
                                foreach($pageData as $row){ ?>
                                    <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>
                                        <td><?= $row['country']; ?></td>
                                        <td><?= $row['company']; ?></td>
                                        <td><?= $row['details']; ?></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 = array_key_exists($active_warehouse,$row['stats']['stock_in_units']) ? $row['stats']['stock_in_units'][$active_warehouse] : 0;
                                        $item_stock_out_units = array_key_exists($active_warehouse,$row['stats']['stock_out_units']) ? $row['stats']['stock_out_units'][$active_warehouse] : 0;

                                        $item_branch_transfer_in_units = array_key_exists($active_warehouse,$row['stats']['branch_transfer_in_units']) ? $row['stats']['branch_transfer_in_units'][$active_warehouse] : 0;
                                        $item_branch_transfer_out_units = array_key_exists($active_warehouse,$row['stats']['branch_transfer_out_units']) ? $row['stats']['branch_transfer_out_units'][$active_warehouse] : 0;

                                        $item_ph_transfer_in_units = array_key_exists($active_warehouse,$row['stats']['transfer_in_pharma']) ? $row['stats']['transfer_in_pharma'][$active_warehouse] : 0;
                                        $item_ph_transfer_out_units = array_key_exists($active_warehouse,$row['stats']['transfer_out_pharma']) ? $row['stats']['transfer_out_pharma'][$active_warehouse] : 0;

                                        $bundle_stock_in_units = array_key_exists($active_warehouse,$row['stats']['bundle_stock_in_units']) ? $row['stats']['bundle_stock_in_units'][$active_warehouse] : 0;
                                        $bundle_stock_out_units = array_key_exists($active_warehouse,$row['stats']['bundle_stock_out_units']) ? $row['stats']['bundle_stock_out_units'][$active_warehouse] : 0;
                                        $bundle_ava_units = $bundle_stock_in_units - $bundle_stock_out_units;

                                        $toop_stock_in_units = array_key_exists($active_warehouse,$row['stats']['toop_stock_in_units']) ? $row['stats']['toop_stock_in_units'][$active_warehouse] : 0;
                                        $toop_stock_out_units = array_key_exists($active_warehouse,$row['stats']['toop_stock_out_units']) ? $row['stats']['toop_stock_out_units'][$active_warehouse] : 0;
                                        $toop_ava_units = $toop_stock_in_units - $toop_stock_out_units;


                                        $item_damage_units = array_key_exists($active_warehouse,$row['stats']['damage_units']) ? $row['stats']['damage_units'][$active_warehouse] : 0;
                                        $item_avail_units = array_key_exists($active_warehouse,$row['stats']['avail_units']) ? $row['stats']['avail_units'][$active_warehouse] : 0;

                                        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);
                                        }
                                        
                                        ?>
                                        
                                        <td><?= round_decimels_digits( $item_avg_cost ); ?></td>
                                        <td><?= round_decimels_digits( $item_last_cost ); ?></td>
                                        <td><?= round_decimels_digits( $item_sale_price ); ?></td>
                                        
                                        <td><?= round_decimels_digits( $item_stock_in_units ); $w_purchases_totals[] = $item_stock_in_units; ?></td>
                                        <td><?= round_decimels_digits( $item_stock_out_units ); $w_sales_totals[] = $item_stock_out_units; ?></td>
                                        <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                            <td><?= round_decimels_digits( $item_branch_transfer_in_units ); $w_btin_totals[] = $item_branch_transfer_in_units; ?></td>
                                            <td><?= round_decimels_digits( $item_branch_transfer_out_units ); $w_btout_totalst[] = $item_branch_transfer_out_units; ?></td>
                                        <?php } ?>
                                        <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                            <td><?= round_decimels_digits( $item_ph_transfer_in_units ); $w_tin_totals[] = $item_ph_transfer_in_units; ?></td>
                                            <td><?= round_decimels_digits( $item_ph_transfer_out_units ); $w_tout_totalst[] = $item_ph_transfer_out_units; ?></td>
                                        <?php } ?>

                                        <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                                            <td><?= round_decimels_digits($bundle_ava_units); $w_bundle_totals[] = $bundle_ava_units; ?></td>
                                            <td><?= round_decimels_digits($toop_ava_units); $w_toop_totals[] = $toop_ava_units; ?></td>
                                        <?php } ?>
                                        <td><?= round_decimels_digits( $item_damage_units ); $w_damage_totals[] = $item_damage_units; ?></td>
                                        <td><?= round_decimels_digits( $item_avail_units ); $w_ava_totals[] = $item_avail_units; ?></td>
                                        <td><?= round_decimels_digits( $item_avail_units * $item_avg_cost ); $w_value_totals[] = ($item_avail_units * $item_avg_cost); ?></td>

                                        <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>
                                            <?php if(check_user_access('add_warehouse_transfer') === TRUE){ ?>
                                                <a href="<?= url('/transfer/add').'?warehouse='.$active_warehouse.'&item='.$row['uid']; ?>" class="btn btn-xs btn-primary url-elem"><i class="fa fa-exchange">&nbsp; </i><?= print_lang($app_lang,'transfer'); ?></a>
                                            <?php } ?>
                                        </td>
                                    </tr>
                                <?php } ?>

                                    <tr class="text-center">
                                        <td><?= $i++; ?></td>
                                        <td><?= print_lang($app_lang,'total'); ?></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        
                                        <td><?= amount_number_format( array_sum($w_purchases_totals) ); ?></td>
                                        <td><?= amount_number_format( array_sum($w_sales_totals) ); ?></td>
                                        <?php if($item_settings['transfer'] == 'b_transfer'){ ?>
                                            <td><?= amount_number_format( array_sum($w_btin_totals) ); ?></td>
                                            <td><?= amount_number_format( array_sum($w_btout_totalst) ); ?></td>
                                        <?php } ?>
                                        <?php if($item_settings['transfer'] == 'n_transfer'){ ?>
                                            <td><?= amount_number_format( array_sum($w_tin_totals) ); ?></td>
                                            <td><?= amount_number_format( array_sum($w_tout_totalst) ); ?></td>
                                        <?php } ?>

                                        <?php if($item_settings['bundle_toop'] == 'enable'){ ?>
                                            <td><?= amount_number_format( array_sum($w_bundle_totals) ); ?></td>
                                            <td><?= amount_number_format( array_sum($w_toop_totals) ); ?></td>
                                        <?php } ?>
                                        <td><?= amount_number_format( array_sum($w_damage_totals) ); ?></td>
                                        <td><?= amount_number_format( array_sum($w_ava_totals) ); ?></td>
                                        <td><?= amount_number_format( array_sum($w_value_totals) ); ?></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                            </tbody>
                        </table>    
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>