createQuery('a') ->where('a.card_id = ?', $this->id) ->execute(); if ($this->state <> NULL) { $stav = 'Uzavřené - ' . $this->state . ', ' . $cena[0]->price . ' Kč'; } if ($this->user_id <> NULL AND $this->user_finished == 1 AND $this->state == NULL) { $stav = 'Uzavřené uživatelem'; } if ($this->user_finished == 0) { $stav = 'Aktivní'; } if ($this->user_id == NULL) { $stav = 'Nově importované'; } return $stav; } public function __toString() { return "{$this->client_name} ({$this->client_code})"; } public function getAssignedUsername() { if (!empty($this->user_id)) return Doctrine::getTable('sfGuardUser')->find($this->user_id)->username; else return false; } public function getAssignedFullname() { if (!empty($this->user_id)) { $profil = Doctrine::getTable('sfGuardUserProfile')->find($this->user_id); return ($profil->first_name . ' ' . $profil->last_name); } else return false; } public function startDate() { $start_date = date("Y-m-d H:i:s"); if (count($this->Attachments)) { foreach ($this->Attachments AS $attachment) if (strtotime($attachment->requisition_date) < strtotime($start_date)) $start_date = $attachment->requisition_date; } else { $start_date = $this->created_at; } return $start_date; } public function caseDays() { $start = new sfDate($this->startDate()); $end = new sfDate($this->state_date); $days = $end->diff($start, sfTime::DAY); return $days; } public function lastActivityDate() { $message = Doctrine::getTable('Message') ->createQuery('m') ->where('m.card_id = ?', $this->id) ->orderBy('m.created_at DESC') ->limit(1) ->execute(); if (count($message) == 1) return $message[0]->created_at; return false; } // vypocet celkoveho aktualniho dluhu public function getBalance($type = 'first') { $balance = array( 'price_payment' => 0, 'total_balance' => 0, 'total_payment' => 0, 'total_insurance' => 0, 'total_fee' => 0, 'payed' => 0, 'penalty' => 0, 'debt_id' => -1, 'sum_supplier' => 0, 'sum' => 0, 'month' => '', 'next_refund' => '', 'installment' => 0 ); if (count($this->Attachments)) { foreach ($this->Attachments AS $attachment) { $debt_q = Doctrine::getTable('Debt') ->createQuery() ->where('attachment_id = ?', $attachment->id) ->limit(1); if ($type == 'first') $debt_q->orderBy('id ASC'); elseif ($type == 'last' || $type == 'new') $debt_q->orderBy('id DESC'); $debt = $debt_q->execute(); if (count($debt) == 1) { $debt = $debt[0]; $balance['debt_id'] = $debt->id; $balance['penalty'] = $debt->penalty; $balance['total_insurance'] += $debt->insurance; $balance['total_fee'] += $debt->fee; $balance['total_payment'] += $debt->payment; if ($type == 'new') { $d = new sfDate($debt->update_date); $d->add(1, sfTime::MONTH); $balance['rozdil'] = $this->rozdilMesicu($attachment->id, 'new'); $balance['installment'] = $attachment->price_payment * $this->rozdilMesicu($attachment->id, 'new'); $balance['month'] = $d->format("Y/m"); if (!empty($this->due_day)) { $d1 = new sfDate($debt->update_date); $d1->setDay($attachment->Card->due_day); $d1->add(1, sfTime::MONTH); $d2 = new sfDate($debt->update_date); $d2->setDay(1); $d2->add(1, sfTime::MONTH); $posledni_den = cal_days_in_month(CAL_GREGORIAN, $d2->getMonth(), $d2->getYear()); $d2->setDay($posledni_den); $datum = min($d1, $d2); $balance['next_refund'] = $datum->date(); } } else { $balance['rozdil'] = $this->rozdilMesicu($attachment->id, 'old'); $balance['installment'] += $attachment->price_payment * $this->rozdilMesicu($attachment->id, 'old'); $balance['month'] = date("Y/m", strtotime($debt->update_date)); if (!empty($this->due_day)) { $d1 = new sfDate($debt->update_date); $d1->setDay($this->due_day); $d1->add(1, sfTime::MONTH); $balance['next_refund'] = $d1->date(); } } } else { return false; } } // foreach if ($type == 'first') { $balance['sum_supplier'] = $balance['total_payment'] + $balance['total_insurance'] + $balance['total_fee'] + $balance['penalty']; } else { $balance['sum_supplier'] = $balance['total_payment'] + $balance['total_insurance'] + $balance['total_fee'] + $balance['installment'] + $balance['penalty']; } // najdeme default vypocet provize pro zadavatele $costs = 0; $default = Doctrine::getTable('Supplier_state_def') ->createQuery() ->where('supplier_id = ?', $this->supplier_id) ->andWhere('is_default = 1') ->execute(); if (count($default) == 1) { if (!empty($default[0]->costs_price)) $costs = $default[0]->costs_price; else $costs = $balance['sum_supplier'] * ($default[0]->costs_percent / 100); } $balance['costs'] = $costs; $balance['payments'] = $this->getPaymentsSum(); $balance['sum'] = $balance['sum_supplier'] + $balance['costs'] - $balance['payments']; $balance['type'] = $type; return $balance; } return false; } public function rozdilMesicu($id , $type) { $prvni = Doctrine::getTable('Debt') ->createQuery() ->where('attachment_id = ?', $id) ->limit(1) ->orderBy('id ASC') ->execute(); $posledni = Doctrine::getTable('Debt') ->createQuery() ->where('attachment_id = ?', $id) ->limit(1) ->orderBy('id DESC') ->execute(); $d = new sfDate($prvni[0]->update_date); $rok1 = $d->getYear(); $mesic1 = $d->getMonth(); $d2 = new sfDate($posledni[0]->update_date); if ($type == 'new') { $d2->add(1, sfTime::MONTH); } $rok2 = $d2->getYear(); $mesic2 = $d2->getMonth(); $vysledek = (($rok2 * 12) + $mesic2) - (($rok1 * 12) + $mesic1); return $vysledek; } public function getAllCosts() { // vypocet nakladu $total = 0; foreach ($this->Attachments AS $attachment) { $debt_q = Doctrine::getTable('Debt') ->createQuery() ->where('attachment_id = ?', $attachment->id) ->orderBy('id DESC') ->limit(1); $debt = $debt_q->execute(); if (count($debt) == 1) { $debt = $debt[0]; $total += $debt->payment; $total += $debt->insurance; $total += $debt->fee; $total += $debt->penalty; } } $card_enum = array_flip(CardTable::$stateEnum); $all_costs = array(); foreach ($this->Supplier->State_defs AS $def) { $all_costs[$def->name] = !empty($def->costs_percent) ? ($total * $def->costs_percent / 100) : $def->costs_price; } return $all_costs; } public function getAttachmentCodes($string = false) { if (count($this->Attachments)) { $codesArr = array(); foreach ($this->Attachments AS $attachment) { $codesArr[] = $attachment->supplier_code; } if ($string === false) return $codesArr; else return implode(', ', $codesArr); } return false; } public function getPaymentsSum() { $sum = 0; if (count($this->Attachments)) { foreach ($this->Attachments AS $attachment) { $payments = Doctrine::getTable('Payment') ->createQuery() ->where('attachment_id = ?', $attachment->id) ->orderBy('payment_date') ->execute(); $PREPLATEK = 0; foreach ($attachment->Payments AS $payment) { if ($payment->payment_type != 'PREPLATEK') $sum += $payment->price; else $PREPLATEK = $payment->price; } $sum += $PREPLATEK; } } return $sum; } /* * Lucene fulltext */ public function updateLuceneIndex() { $index = $this->getTable()->getLuceneIndex(); // remove existing entries foreach ($index->find('pk:' . $this->getId()) as $hit) { $index->delete($hit->id); } $doc = new Zend_Search_Lucene_Document(); // store job primary key to identify it in the search results $doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId())); // index job fields $doc->addField(Zend_Search_Lucene_Field::UnStored('client_code', $this->client_code, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('client_name', $this->client_name, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('cell', $this->cell, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('phone', $this->phone, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('phone_note', $this->phone_note, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_street', $this->address_street, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_city', $this->address_city, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_zipcode', $this->address_zipcode, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_cell', $this->address_cell, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_phone', $this->address_phone, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('address_phone2', $this->address_phone2, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_street', $this->altaddress_street, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_city', $this->altaddress_city, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_zipcode', $this->altaddress_zipcode, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_cell', $this->altaddress_cell, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_phone', $this->altaddress_phone, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('altaddress_phone2', $this->altaddress_phone2, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer', $this->employer, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer_position', $this->employer_position, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer_phone', $this->employer_phone, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer_street', $this->employer_street, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer_city', $this->employer_city, 'UTF-8')); $doc->addField(Zend_Search_Lucene_Field::UnStored('employer_zipcode', $this->employer_zipcode, 'UTF-8')); // add job to the index $index->addDocument($doc); $index->commit(); } // pretizeni ukladani pro zpracovani indexu public function save(Doctrine_Connection $conn = null) { $ret = parent::save($conn); $this->updateLuceneIndex(); return $ret; } }