*/ protected function filterDate($date) { switch (true) { case (!$date || !strlen(trim(implode('', (array) $date)))): case (is_array($date) && (!isset($date['year']) || !isset($date['month']) || !isset($date['day']))): // $date is null or is an array with empty or zero string elements // or one of its components is not set (year, month, day) return null; case (is_array($date)): // is an array and is not bad formed return $date['year'].'/'.$date['month'].'/'.$date['day']; default: // is not an array return $date; } } /** * Limits the results to those invoices issued in a date greater or equal than that * one passed as parameter. * @param mixed date value * @return InvoiceFinder the same instance * @author Carlos Escribano */ public function fromDate($date = null) { if (!($date = $this->filterDate($date))) { return $this; } else { return $this->where('IssueDate', '>=', sfDate::getInstance($date)->setHour(0)->setMinute(0)->setSecond(0)->to_database()); } } /** * Limits the results to those invoices issued in a date smaller or equal than that * one passed as parameter. * @param mixed date value * @return InvoiceFinder the same instance * @author Carlos Escribano */ public function toDate($date = null) { if (!($date = $this->filterDate($date))) { return $this; } else { return $this->where('IssueDate', '<=', sfDate::getInstance($date)->setHour(23)->setMinute(59)->setSecond(59)->to_database()); } } /** * Limits the results to those invoices opened that: * - are overdue, if parameter is true * - are not overdue, if parameter is false * @param boolean * @return InvoiceFinder the same instance * @author Carlos Escribano */ public function overdue($is = true) { return $this->opened()->where('DueDate', ($is ? '<' : '>='), sfDate::getInstance()->setHour(23)->setMinute(59)->setSecond(59)->to_database()); } /** * Limits the results to those invoices opened that: * - are opened, if parameter is true * - are not opened, if parameter is false * @param boolean * @return InvoiceFinder the same instance * @author Carlos Escribano */ public function opened($is = true) { return $this->where('Due', ($is ? '>' : '<='), 0); } /** * Returns a total value based on a field name. * It preserves the query to reuse and tune it to get another results. * @param string $field Field name (DbFinder format) * @return mixed single value * @author Carlos Escribano */ public function sum($field = 'Gross') { return $this->withColumn("SUM($field)" , 'total') ->select('total') ->findOne(); } /** * @param array $search * Common parameters to all Invoice objects: * 'query' -> text fields query * 'tags' -> tag search * 'sort' -> array with field and method (asc, desc) common ^ * ---------------------------------------------------------------- * 'from' & 'to' -> date range parameters specific . * @return DbFinder's same instance * @author Carlos Escribano */ public function search($search) { $this->commonSearch($search); if(isset($search['query']) && strlen($search['query'])) { $ids = DbFinder::from('InvoiceItem') ->select('InvoiceId', sfModelFinder::SIMPLE) ->where('Description', 'like', "%".$search['query']."%") ->find(); $this->where('Id', 'in', $ids, 'description') ->combine(array('invoice_conditions', 'description'), 'or') ->distinct(); // If we use InvoiceItem.Description directly dbFinder will add an INNER JOIN // that will bring us wrong calculations. // Carlos. } if (isset($search['from'])) { $this->fromDate($search['from']); } if (isset($search['to'])) { $this->toDate($search['to']); } if (isset($search['status'])) { switch(strtolower($search['status'])) { case 'opened': $this->opened(true); break; case 'sent': $this->where('SentByEmail', '=', true); break; case 'closed': $this->opened(false); break; case 'overdue': $this->overdue(true); break; case 'draft': $this->where('Draft', '=', true); break; default: // all break; } } return $this; } public function selectStatus($search) { return $this ->withColumn("if(draft, 'DRAFT', if(due>0, if(due_date