* @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkEdit();
$service_id = CValue::get('service_id');
$keywords = CValue::get('keywords');
$limit = CValue::get('limit');
// Service's stocks
$where = array();
if ($service_id) {
    $where['product_stock_service.object_id'] = " = '{$service_id}'";
    $where['product_stock_service.object_class'] = " = 'CService'";
    // XXX
}
if ($keywords) {
    $where[] = "product.code LIKE '%{$keywords}%' OR \r\n              product.name LIKE '%{$keywords}%' OR \r\n              product.description LIKE '%{$keywords}%'";
}
$orderby = 'product.name ASC';
$leftjoin = array();
$leftjoin['product'] = 'product.product_id = product_stock_service.product_id';
// product to stock
$stock = new CProductStockService();
$list_stocks_count = $stock->countList($where, null, $leftjoin);
$list_stocks = $stock->loadList($where, $orderby, $limit ? $limit : 30, null, $leftjoin);
// Smarty template
$smarty = new CSmartyDP();
$smarty->assign('stock', $stock);
$smarty->assign('list_stocks', $list_stocks);
$smarty->assign('list_stocks_count', $list_stocks_count);
$smarty->display('inc_stocks_list.tpl');
$stock_id = CValue::getOrSession('stock_service_id');
$category_id = CValue::get('category_id');
$object_id = CValue::get('object_id');
$keywords = CValue::get('keywords');
$start = CValue::get('start');
CValue::setSession('category_id', $category_id);
$where = array("service.group_id" => "= '" . CProductStockGroup::getHostGroup() . "'");
if ($object_id) {
    $where['product_stock_service.object_id'] = " = '{$object_id}'";
    $where['product_stock_service.object_class'] = " = 'CService'";
    // XXX
}
if ($category_id) {
    $where['product.category_id'] = " = '{$category_id}'";
}
if ($keywords) {
    $where[] = "product.code LIKE '%{$keywords}%' OR \r\n              product.name LIKE '%{$keywords}%' OR \r\n              product.description LIKE '%{$keywords}%'";
}
$leftjoin = array("product" => "product.product_id = product_stock_service.product_id", "service" => "service.service_id = product_stock_service.object_id");
$stock = new CProductStockService();
$list_stocks_count = $stock->countList($where, null, $leftjoin);
$pagination_size = CAppUI::conf("dPstock CProductStockService pagination_size");
$list_stocks = $stock->loadList($where, 'product.name ASC', intval($start) . ",{$pagination_size}", null, $leftjoin);
// Smarty template
$smarty = new CSmartyDP();
$smarty->assign('stock', $stock);
$smarty->assign('stock_id', $stock_id);
$smarty->assign('list_stocks', $list_stocks);
$smarty->assign('list_stocks_count', $list_stocks_count);
$smarty->assign('start', $start);
$smarty->display('inc_stocks_list.tpl');