/**
  * @param FilterTree|Filter|array $data
  * @return Filter
  */
 public function buildFilter($data)
 {
     if ($data instanceof FilterTree) {
         return $data->buildFilter([$this, 'buildFilter']);
     } else {
         if ($data instanceof Filter) {
             return $data;
         }
     }
     $filter = new Filter();
     $tmp0 = $tmp1 = '';
     if (isset($data['filterCustomerId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('i.customer_id', $data['filterCustomerId'], $tmp0, $tmp1));
     }
     if (isset($data['filterInvoiceId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('i.invoice_id', $data['filterInvoiceId'], $tmp0, $tmp1));
     }
     if (isset($data['filterInvoiceStatusId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('i.invoice_status_id', $data['filterInvoiceStatusId'], $tmp0, $tmp1));
     }
     return $filter;
 }
 /**
  * @param FilterTree|Filter|array $data
  * @return Filter
  */
 public function buildFilter($data)
 {
     if ($data instanceof FilterTree) {
         return $data->buildFilter([$this, 'buildFilter']);
     } else {
         if ($data instanceof Filter) {
             return $data;
         }
     }
     $filter = new Filter();
     $tmp0 = $tmp1 = '';
     if (isset($data['selectedItems'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p.product_id', $data['selectedItems'], $tmp0, $tmp1));
     }
     if (!empty($data['filterCategoryId'])) {
         $categories = [];
         $categories[] = $data['filterCategoryId'];
         if (!empty($data['filterSubCategories'])) {
             foreach (CategoryDAO::getInstance()->getCategoriesByParentId($data['filterCategoryId']) as $category) {
                 $categories[] = $category->getId();
             }
         }
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p2c.category_id', $categories, $tmp0, $tmp1));
     }
     if (!empty($data['filterDateAddedFrom'])) {
         $filter->addChunk("p.date_added > :dateAddedFrom", [":dateAddedFrom" => $data['filterDateAddedFrom']]);
     }
     if (!empty($data['filterDateAddedTo'])) {
         $filter->addChunk("p.date_added < :dateAddedTo", [":dateAddedTo" => date('Y-m-d', strtotime($data['filterDateAddedTo']) + 86400)]);
     }
     if (isset($data['filterEnabled'])) {
         $filter->addChunk("p.status = :enabled", [":enabled" => $data['filterEnabled']]);
     }
     if (!empty($data['filterId']) && is_numeric($data['filterId'])) {
         $filter->addChunk('p.product_id = :productId', [':productId' => $data['filterId']]);
     }
     if (!empty($data['filterKoreanName'])) {
         $filter->addChunk("p.korean_name LIKE CONCAT('%', :koreanName, '%')", [':koreanName' => $data['filterKoreanName']]);
     }
     if (!empty($data['filterLanguageId'])) {
         $filter->addChunk("pd.language_id = :languageId", [":languageId" => $data['filterLanguageId']]);
     }
     if (!empty($data['filterManufacturerId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p.manufacturer_id', $data['filterManufacturerId'], $tmp0, $tmp1));
     }
     if (!empty($data['filterModel'])) {
         $filter->addChunk("p.model LIKE CONCAT('%', :model, '%')", [':model' => $data['filterModel']]);
     }
     if (!empty($data['filterName'])) {
         $words = explode(' ', $data['filterName']);
         $filterString = '';
         $filterParams = [];
         for ($i = 0; $i < sizeof($words); $i++) {
             $filterString .= " OR pd.name LIKE CONCAT('%', :name{$i}, '%') OR pd.description LIKE CONCAT('%', :name{$i}, '%')";
             $filterParams[":name{$i}"] = $words[$i];
         }
         $filter->addChunk("(" . substr($filterString, 4) . ")", $filterParams);
     }
     if (!empty($data['filterPriceRange'])) {
         $filterString = '';
         if (!is_null($data['filterPriceRange'][0])) {
             $filterString = " AND p.price >= :priceFrom";
         }
         if (!is_null($data['filterPriceRange'][1])) {
             $filterString .= "AND p.price <= :priceTo";
         }
         $filter->addChunk(substr($filterString, 4), [':priceFrom' => $data['filterPriceRange'][0], ':priceTo' => $data['filterPriceRange'][1]]);
     }
     if (!empty($data['filterStoreId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p2s.store_id', $data['filterStoreId'], $tmp0, $tmp1));
     }
     if (!empty($data['filterSupplierId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p.supplier_id', $data['filterSupplierId'], $tmp0, $tmp1));
     }
     if (!empty($data['filterTag'])) {
         $words = explode(' ', $data['filterTag']);
         $filterString = '';
         $filterParams = [];
         for ($i = 0; $i < sizeof($words); $i++) {
             $filterString .= " OR pt.tag LIKE CONCAT('%', :tag{$i}, '%')";
             $filterParams[":tag{$i}"] = $words[$i];
         }
         $filter->addChunk("(" . substr($filterString, 4) . ")", $filterParams);
     }
     if (!empty($data['filterUserNameId']) && is_array($data['filterUserNameId']) && sizeof($data['filterUserNameId'])) {
         $filter->addChunk($this->buildSimpleFieldFilterEntry('p.user_id', $data['filterUserNameId'], $tmp0, $tmp1));
         //            $iDSet = array();
         //            $filterUserName = array();
         //            foreach ($data['filterUserNameId'] as $usernameId)
         //                if ($usernameId)
         //                    $iDSet[] = $usernameId;
         //                else
         //                    $filterUserName['null'] = "u.user_id IS NULL";
         //            if (sizeof($iDSet))
         //                $filterUserName['ids'] = "u.user_id IN (" . implode(', ', $iDSet) . ")";
         //            $filter .= ($filter ? " AND" : "") . ' (' . implode(' OR ', $filterUserName) . ')';
     }
     return $filter;
 }