<?php // BEGIN PHP
session_start(); // start session
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/lib/categories.lib.php';
include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
require_once DOL_DOCUMENT_ROOT."/commande/class/commande.class.php";

$tag = GETPOST('tag', 'alpha');
$cat_id = GETPOST('cat', 'alpha');
$search = GETPOST('search_query', 'alpha');
$product_id = GETPOST('id');
$current_lang = $_SESSION['lang']['code'];
$weblangs->load("companies");


// If not login redirect to login.
if(empty($_SESSION['user']['id_user'])){
    redirectToContainer('login-register');
}


// Get Account ID.
$user_id = $_SESSION['user']['id_user'];
$customer_account = new SocieteAccount($db);
$result = $customer_account->fetch($user_id);
$customer_id = $customer_account->fk_soc;

// Get customer ID.
$customer = new Societe($db);
$result = $customer->fetch($customer_id);

// Check if this product is owned by this customer
$product = new Product($db);
$result = $product->fetch($product_id);
$sql = 'SELECT count(rowid) as prices FROM '.MAIN_DB_PREFIX.'product_fournisseur_price WHERE fk_product = '.((int) $product_id).' and fk_soc = '.((int) $customer_id);
$resql = $db->query($sql);
$is_owner = $db->fetch_object($resql);
if ($is_owner->prices == 0) {
    // To render view of another user
    if (!empty(GETPOST('id_customer'))) {
        // Only for members with a specific role
        require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php";
        $membre = new Adherent($db);
        $result = $membre->fetch('', 0, $customer_id);
        
        $adminsCat = new Categorie($db);
    	$adminsCat->fetch(1);
    	
    	$result = $adminsCat->containsObject('member', $membre->id);
    	if ($result <= 0) {
            redirectToContainer('manage-products');
    	}
    } else {
        redirectToContainer('manage-products');
    }
}
// END PHP 
?>
<div id="page">
  <div class="header-container">
    <?php 
        includeContainer('header');
        $__PAGE__TITLE__ = $weblangs->trans("productStatisticsPageTitle");
    ?>
  </div>

  <div class="columns-container">
    <div id="columns" class="container">
        <!--  Breadcrumb  -->
        <div class="breadcrumb clearfix">
        	<a class="home" href="index.php" title="Return to Home"><i class="icon-home"></i></a>
        	<span class="navigation-pipe">&gt;</span> <a href="myaccount.php" title="<?php print $weblangs->trans("myAccount"); ?>" data-gg=""><?php print $weblangs->trans("manageProducts"); ?></a>
        	<span class="navigation-pipe">&gt;</span> <?php print $weblangs->trans("orderHistory"); ?>
        </div>
        <!--  Breadcrumb  -->
      <div id="slider_row" class="row">
        <div id="top_column" class="center_column col-xs-12 col-sm-12"></div>
      </div>
      <div class="row">
        <!-- include lefet sidebar container -->
        <?php includeContainer('sidebar'); ?>
        <div id="center_column" class="center_column col-xs-12 col-sm-9">
          <div class="clearfix">
                <?php
                    // Get product object
                    $product = new Product($db);
                    $result = $product->fetch($product_id);
                ?>
                <h1 class="page-heading"><?php print $weblangs->trans("product"); ?> : <?php print $product->label; ?></h1>
                <div id="block-history" class="block-center">
                    <div id="block-order-detail" class="unvisible" style="display: block;">
                		<div id="order-detail-content" class="table_block table-responsive">
                			<table class="table table-bordered table-bordered-block-order-detail order-history">
                				<thead>
                					<tr>
                						<th class="item" width="50">#</th>
                						<th class="item" width="150"><?php print $weblangs->trans("orderRef"); ?></th>
                						<th class="item"><?php print $weblangs->trans("ThirdPartyName"); ?></th>
                						<th class="item" width="120"><?php print $weblangs->trans("date"); ?></th>
                						<th class="last_item"><?php print $weblangs->trans("amountEarned"); ?></th>
                					</tr>
                				</thead>
                				<tbody>
                				    <?php
                				    // Get list of orders for this product 
                					$orders_for_product = "SELECT c.rowid, c.ref, c.fk_statut, c.fk_soc, c.date_commande, c.module_source , d.total_ht ";
                                    $orders_for_product .= "FROM ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."commandedet as d ";
                                    $orders_for_product .= "WHERE c.rowid = d.fk_commande and d.fk_product = " . ((int) $product->id) . " and c.fk_statut IN (1,3) and (c.facture = 1 || c.ref_ext IS NOT NULL) and c.module_source = 'Marketplace' ";
                                    if ($result_orders = $db->query($orders_for_product)) {
                                        $i = 1;
                                    	while ($order_details = $result_orders->fetch_object()) {
                                    	    if (!empty($conf->cache['thirdparty'][$order_details->fk_soc])) {
                                    	        $customer = $conf->cache['thirdparty'][$order_details->fk_soc];
                                    	    } else {
                                    	        $customer = new Societe($db);
                                    	        $customer->fetch($order_details->fk_soc);
                                    	    
                                    	        $conf->cache['thirdparty'][$order_details->fk_soc] = $customer;
                                    	    }
                                    	    
                                    	    // Calculate discount for this order line
                                            $TOTAL_DISCOUNTS = 0;
                                            $sum_all_discounts = "SELECT SUM(d.total_ht) as total ";
                                            $sum_all_discounts .= "FROM ".MAIN_DB_PREFIX."commande as c, ".MAIN_DB_PREFIX."commandedet as d ";
                                            $sum_all_discounts .= "WHERE c.rowid = d.fk_commande ";
                                            $sum_all_discounts .= "AND c.rowid = ".$order_details->rowid." ";
                                            $sum_all_discounts .= "AND d.description LIKE '" . ((int) $product->id) . "# %' ";
                                            $sum_all_discounts .= "AND (c.facture = 1 OR c.note_private LIKE '%Order imported%' OR c.ref_ext IS NOT NULL) ";
                                            $sum_all_discounts .= "AND c.module_source = 'Marketplace' ";
                                            $resql = $db->query($sum_all_discounts);
                                            $sells_discounts = $db->fetch_object($resql);
                                            $TOTAL_DISCOUNTS = $sells_discounts->total;
                                    ?>
                    					<tr class="item">
                    					    <td>
                    					        <p><?php print $i; ?></p>
                    					    </td>
                    					    <td>
                    					        <p><?php print $order_details->ref; ?></p>
                    					    </td>
                    					    <td>
                    						        <b><?php print $customer->name; ?></b><br>

                    						        <small class="cart_ref">
                                                        <?php print $weblangs->trans("Email"); ?>: <?php print $customer->email; ?><br>
                                                        <?php print $weblangs->trans("registredOn"); ?>: <?php print dol_print_date($customer->date_creation, 'day'); ?>
                    						        </small>

                    						</td>
                    						<td>
                    						    <p><?php print dol_print_date($order_details->date_commande, 'day'); ?></p>
											</td>
                    						<td width="160">
                    							<p><span class="price"><?php print price($order_details->total_ht); ?> €</span> <?php if ($TOTAL_DISCOUNTS) { print "(" . price($TOTAL_DISCOUNTS) . ")"; } ?> </p>
                    						</td>
                    					</tr>
                					    <?php
                					    $i++;
                                    	}
                                    }
                                    ?>
                				</tbody>
                			</table>
                		</div>
                    </div>
                </div>
                <ul class="footer_links clearfix">
                	<li>
                		<a class="btn btn-default button button-small" href="myaccount.php">
                			<span>
                				<i class="icon-chevron-left"></i> <?php print $weblangs->trans("backToYourAccount"); ?>
                			</span>
                		</a>
                	</li>
                	<li>
                		<a class="btn btn-default button button-small" href="index.php">
                			<span><i class="icon-chevron-left"></i> <?php print $weblangs->trans("home"); ?></span>
                		</a>
                	</li>
                </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- .columns-container -->
  <div class="footer-container">
    <?php includeContainer('footer'); ?>
  </div>
</div>