<?php


/**
 * Помощен детайл подготвящ и обединяващ заедно детайлите за изпозлванията на артикула в документа
 *
 * @category  bgerp
 * @package   cat
 *
 * @author    Ivelin Dimov <ivelin_pdimov@abv.bg>
 * @copyright 2006 - 2017 Experta OOD
 * @license   GPL 3
 *
 * @since     v 0.1
 */
class cat_products_Usage extends core_Manager
{
    /**
     * Колко да са на страница заданията
     */
    public $listJobsPerPage = 10;
    
    
    /**
     * Колко да са на страница другите документи
     */
    public $listOtherDocumentsPerPage = 10;
    
    
    /**
     * Подготвя ценовата информация за артикула
     */
    public function prepareUsage($data)
    {
        $masterRec = $data->masterData->rec;
        $data->isPublic = ($masterRec->isPublic == 'yes');
        $tabParam = $data->masterData->tabTopParam;
        $prepareTab = Request::get($tabParam);

        $data->jobData = clone $data;
        $data->jobData->Jobs = cls::get('planning_Jobs');
        $this->prepareJobs($data->jobData);

        if(haveRole('ceo,planning,jobSee')){
            $data->taskData = clone $data;
            $data->taskData->_useMasterField = 'productId';
            $this->prepareDocuments('planning_Tasks', 'planning_ProductionTaskProducts', $data->taskData);
        }

        // Промяна на таба взависимост дали артикула е стандартен или не
        if ($data->isPublic === true) {
            if ((($masterRec->state == 'template') || ($data->jobData->notManifacturable === true)) && !countR($data->jobData->rows) && !countR($data->taskData->rows)) {

                return;
            }
            if (!haveRole('ceo,planning,jobSee')) {

                return;
            }

            $countTasks = countR($data->taskData->rows);
            if($data->masterData->rec->innerClass == planning_interface_StepProductDriver::getClassId() && !$countTasks){

                return;
            }
            $data->Tab = 'top';
            $data->TabCaption =  $countTasks ? 'Документи' : 'Задания';

            if (!$prepareTab || $prepareTab != 'Usage') {

                return;
            }
        } else {
            $data->Tab = 'top';
            $data->TabCaption = 'Документи';
            $data->Order = 2;
            if ($prepareTab && $prepareTab != 'Usage') {
                
                return;
            }
        }

        // Ако артикула е нестандартен тогава се показват и документите, в които се използват
        if ($data->isPublic === false) {
            $data->saleData = clone $data;
            $this->prepareDocuments('sales_Sales', 'sales_SalesDetails', $data->saleData);
            
            $data->purData = clone $data;
            $this->prepareDocuments('purchase_Purchases', 'purchase_PurchasesDetails', $data->purData);
            
            $data->quoteData = clone $data;
            $this->prepareDocuments('sales_Quotations', 'sales_QuotationsDetails', $data->quoteData);

            $data->purQuoteData = clone $data;
            $this->prepareDocuments('purchase_Quotations', 'purchase_QuotationDetails', $data->purQuoteData);
        }
    }
    
    
    /**
     * Рендира ценовата информация за артикула
     */
    public function renderUsage($data)
    {
        $tpl = new core_ET('');
        $tpl->append($this->renderJobs($data->jobData));
        
        if ($data->isPublic === false) {
            $renderEvenIfEmpty = ($data->jobData->notManifacturable === true);
            $tpl->append($this->renderDocuments($data->saleData, $renderEvenIfEmpty));
            $tpl->append($this->renderDocuments($data->purData));
            $tpl->append($this->renderDocuments($data->quoteData));
            $tpl->append($this->renderDocuments($data->purQuoteData));
        }
        $tpl->append($this->renderDocuments($data->taskData));

        return $tpl;
    }
    
    
    /**
     * Подготовка на използвания в документ
     *
     * @param mixed    $Document       - Документ
     * @param mixed    $DocumentDetail - Детайл на документ
     * @param stdClass $data           - Данни
     */
    private function prepareDocuments($Document, $DocumentDetail, &$data)
    {
        $data->Document = clone cls::get($Document);
        $data->Document->FNC('title', 'varchar', 'tdClass=leftCol,forceField');

        $Detail = cls::get($DocumentDetail);
        $data->recs = $data->rows = array();
        
        // Извличане на документите в чиито детайл се среща
        $dQuery = $Detail->getQuery();
        $dQuery->EXT('stateMaster', $Document, "externalName=state,externalKey={$Detail->masterKey}");
        $dQuery->where("#productId = {$data->masterId} AND #stateMaster != 'rejected'");
        $dQuery->show($Detail->masterKey);

        $ids = arr::extractValuesFromArray($dQuery->fetchAll(), $Detail->masterKey);

        if($data->_useMasterField){
            $query2 = $data->Document->getQuery();
            $query2->where("#{$data->_useMasterField} = {$data->masterId} AND #state != 'rejected'");
            $ids += arr::extractValuesFromArray($query2->fetchAll(), 'id');
        }

        $data->Pager = cls::get('core_Pager', array('itemsPerPage' => $this->listOtherDocumentsPerPage));
        $data->Pager->setPageVar('cat_Products', $data->masterId, $Document);
        
        // Ограничаване на заявката
        $query = $data->Document->getQuery();
        $query->XPR('orderByState', 'int', "(CASE #state WHEN 'active' THEN 1 WHEN 'closed' THEN 2  WHEN 'pending' THEN '3' ELSE 4 END)");
        $query->orderBy('#orderByState=ASC,#id=DESC');
        
        if (countR($ids)) {
            $query->in('id', $ids);
        } else {
            $query->where('1=2');
        }
        
        $data->Pager->setLimit($query);
        $fields = $data->Document->selectFields();
        $fields['-list'] = true;
        
        // Вербализиране на записите
        while ($dRec = $query->fetch()) {
            $data->recs[$dRec->id] = $dRec;
            $row = $data->Document->recToVerbal($dRec, $fields);
            $row->title = $data->Document->getHyperlink($dRec->id, true);
            $row->created = "{$row->createdOn} " . tr('от||by') . " {$row->createdBy}";
            $data->rows[$dRec->id] = $row;
        }
    }
    
    
    /**
     * Рендира таблицата с документите
     *
     * @param stdClass $data
     * @param bool     $evenIfEmpty
     *
     * @return void|core_ET
     */
    private function renderDocuments($data, $evenIfEmpty = false)
    {
        if (!countR($data->rows) && $evenIfEmpty === false) {
            
            return;
        }
        
        $tpl = getTplFromFile('crm/tpl/ContragentDetail.shtml');
        $tpl->replace("style='margin-top:10px'", 'STYLE');
        $title = tr($data->Document->title);
        $tpl->append($title, 'title');
        
        $data->listFields = arr::make("title={$data->Document->singleTitle},folderId=Папка,created=Създадено");
        $dateArr = ($data->Document instanceof sales_Quotations) ? array('date' => 'Дата') : array('valior' => 'Вальор');
        arr::placeInAssocArray($data->listFields, $dateArr, null, 'title');
        $data->listTableMvc = clone $data->Document;
        $data->Document->invoke('BeforeRenderListTable', array($tpl, &$data));
        $data->Document->setFieldType('title', 'varchar');
        $data->Document->setField('title', array('tdClass' => 'leftCell'));
        
        $table = cls::get('core_TableView', array('mvc' => $data->Document));
        $details = $table->get($data->rows, $data->listFields);
        
        $tpl->append($details, 'content');
        if (isset($data->Pager)) {
            $tpl->append($data->Pager->getHtml(), 'content');
        }
        
        $tpl->removePlaces();
        $tpl->removeBlocks();
        
        return $tpl;
    }
    
    
    /**
     * Рендиране на заданията към артикул
     *
     * @param stdClass $data
     *
     * @return core_ET $tpl - шаблон на детайла
     */
    private function renderJobs($data)
    {
        if ($data->hide === true) return;

        $tpl = getTplFromFile('crm/tpl/ContragentDetail.shtml');
        $title = tr('Задания за производство');
        $tpl->append($title, 'title');
        
        if (isset($data->addUrl)) {
            $addBtn = ht::createLink('', $data->addUrl, false, 'ef_icon=img/16/add.png,title=Добавяне на ново задание за производство');
            $tpl->append($addBtn, 'title');
        }
        
        $listFields = arr::make('title=Задание,productId=Артикул,dueDate=Падеж,saleId=Продажба,packQuantity=Планирано,quantityProduced=Заскладено,packagingId=Мярка');
        $listFields = core_TableView::filterEmptyColumns($data->rows, $listFields, 'saleId');
        $data->listFields = $listFields;
        $data->Jobs->invoke('BeforeRenderListTable', array($tpl, &$data));
        
        $listTableMvc = clone $data->Jobs;
        $listTableMvc->FLD('title', 'varchar', 'tdClass=leftCell');
        
        $table = cls::get('core_TableView', array('mvc' => $listTableMvc));
        $details = $table->get($data->rows, $data->listFields);
        
        // Ако артикула не е производим, показваме в детайла
        if ($data->notManifacturable === true) {
            $tpl->append(" <span class='red small'>(" . tr('Артикулът не е производим') . ')</span>', 'title');
            $tpl->append('state-rejected', 'TAB_STATE');
        }
        
        $tpl->append($details, 'content');
        if (isset($data->Pager)) {
            $tpl->append($data->Pager->getHtml(), 'content');
        }
        
        $tpl->removePlaces();
        $tpl->removeBlocks();
        
        return $tpl;
    }
    
    
    /**
     * Подготовка на заданията за артикула
     *
     * @param stdClass $data
     */
    private function prepareJobs(&$data)
    {
        if($data->masterData->rec->innerClass == planning_interface_StepProductDriver::getClassId()){
            $data->hide = true;
            return;
        }

        $masterRec = $data->masterData->rec;
        $data->rows = $data->recs = array();
        
        $fields = $data->Jobs->selectFields();
        $fields['__isDetail'] = $fields['-list'] = true;
        
        $data->Pager = cls::get('core_Pager', array('itemsPerPage' => $this->listJobsPerPage));
        $data->Pager->setPageVar('cat_Products', $data->masterId, 'planning_Jobs');
        
        // Намираме неоттеглените задания
        $query = $data->Jobs->getQuery();
        $query->where("#productId = {$data->masterId}");
        $query->where("#state != 'rejected'");
        $query->orderBy('#id=DESC');
        
        while ($rec = $query->fetch()) {
            $data->recs[$rec->id] = $rec;
        }
        
        if($Driver = cat_Products::getDriver($masterRec)){
            $data->recs += $Driver->getLinkedJobRecs($masterRec->id);
        }
        $data->Pager->itemsCount = countR($data->recs);
        
        foreach ($data->recs as $id => $rec){
            if ($data->Pager->isOnPage()) {
                $data->rows[$id] = $data->Jobs->recToVerbal($rec, $fields);
            }
        }
        
        if ($masterRec->canManifacture != 'yes' || $masterRec->generic == 'yes') {
            $data->notManifacturable = true;
        }
        
        if (!haveRole('ceo,planning,jobSee') || ($data->notManifacturable === true && !countR($data->rows)) || $masterRec->state == 'template' || $masterRec->brState == 'template') {
            $data->hide = true;
            
            return;
        }
        
        // Проверяваме можем ли да добавяме нови задания
        if ($data->Jobs->haveRightFor('add', (object) array('productId' => $data->masterId))) {
            $data->addUrl = array('planning_Jobs', 'add', 'productId' => $data->masterId, 'foreignId' => $masterRec->containerId, 'ret_url' => true);
            if($Driver = cat_Products::getDriver($data->masterId)) {
                $productionData = $Driver->getProductionData($data->masterId);
                if(isset($productionData['centerId'])){
                    $data->addUrl['folderId'] = planning_Centers::fetchField($productionData['centerId'], 'folderId');
                }
            }
        }
    }
}
