add(ReadingPeer::IS_BILLED,0); $c->add(ReadingPeer::DONOTBILL,0); $readings = $contract->getReadings($c); if ($readings) { foreach ($readings as $reading) { if ($reading->getIdxdiff()) { $tariffdetail = TariffDetailPeer::getApplicableTariffForReading($contract->getTariff(),$reading); $read = new InvoiceReading(); $read->setReadingId($reading->getId()); $read->setTariffDetailId($tariffdetail->getId()); $read->setMeterId($reading->getMeterId()); $read->setSerialnb($reading->getSerialnb()); $read->setPrvidx($reading->getPrvidx()); $read->setPrvdate($reading->getPrvdate()); $read->setNewidx($reading->getNewidx()); $read->setNewdate($reading->getNewdate()); $read->setIsEstimated($reading->getIsEstimated()); $read->setMultiplyfactor($reading->getMeter()->getMultiplyfactor()); $read->setNbofdays($reading->getNbofdays()); $read->setTariffDetailName($tariffdetail->getName()); $read->setIdxdiff($reading->getIdxdiff()); $read->setConsumption($read->getIdxdiff() * $read->getMultiplyfactor()); $read->setUnitprice($tariffdetail->getAmount()); $read->setAmount($read->getUnitprice() * $read->getConsumption()); $totalAmount += $read->getAmount(); $this->addInvoiceReading($read); $reading->setIsBilled(1); $reading->save(); } } } $tariffs = TariffDetailPeer::getTariffdetails($contract->getTariffId()); foreach ($tariffs as $tariff) { $other = new InvoiceDetail(); if ($tariff->getApplicablefield() == 'invoice') { $other->setTariffDetailId($tariff->getId()); $other->setName($tariff->getName()); $other->setUnit($tariff->getUnits()); $other->setUnitprice($tariff->getAmount()); $other->setAmount($tariff->getAmount()); $totalAmount += $other->getAmount(); $this->addInvoiceDetail($other); } } foreach ($tariffs as $tariff) { $other = new InvoiceDetail(); if ($tariff->getApplicablefield() == 'invoice.totalAmount') { $other->setTariffDetailId($tariff->getId()); $other->setName($tariff->getName()); $other->setUnit($tariff->getUnits()); // $other->setUnitprice(); $other->setAmount($tariff->getAmount()*$totalAmount); $totalAmount += $other->getAmount(); $this->addInvoiceDetail($other); } } $cont = new InvoiceContract(); $cont->setCenterPaymentId($contract->getCenterPaymentId()); $cont->setName($contract->getName()); $cont->setSupplyAddress($contract->getSupplyAddress()); $cont->setSupplyCity($contract->getSupplyCity()); $cont->setSupplyZip($contract->getSupplyZip()); $cont->setBillingAddress($contract->getBillingAddress()); $cont->setBillingCity($contract->getBillingCity()); $cont->setBillingZip($contract->getBillingZip()); $this->addInvoiceContract($cont); $customer = new InvoiceCustomer(); $customer->setCustomerId($contract->getCustomerId()); $customer->setName($contract->getCustomer()->getName()); $customer->setLanguage($contract->getCustomer()->getLanguage()); $this->addInvoiceCustomer($customer); $this->setContractId($contract->getId()); $this->setServiceId($contract->getServiceId()); $this->setTariffId($contract->getTariffId()); $this->setReadingCycleId($cycle->getId()); $this->setTariffname($contract->getTariff()->getName()); $this->setBillingdate(date('Y-m-d')); $max = sfConfig::get('app_initial_max_payment_date'); $this->setDuedate(date('Y-m-d',strtotime($max))); $this->setTotalamount($totalAmount); // $this->checkIsFullypaid(); $this->setIsPrinted(0); $this->setIsCanceled(0); if ($totalAmount) { $this->save(); return 1; } else { return 0; } } public function regenerateReading($readingid) { $totalAmount = ''; $reading = ReadingPeer::retrieveByPk($readingid); $meter = MeterPeer::retrieveByPk($reading->getMeterId()); $c = new Criteria(); $c->add(InvoiceReadingPeer::READING_ID,$readingid); $reads = $this->getInvoiceReadings($c); $read = $reads[0]; $read->setNewidx($reading->getNewidx()); $read->setPrvidx($reading->getPrvidx()); $read->setNewdate($reading->getNewdate()); $read->setPrvdate($reading->getPrvdate()); $read->setIsEstimated($reading->getIsEstimated()); $read->setMultiplyfactor($meter->getMultiplyfactor()); $read->setNbofdays($reading->getNbofdays()); $read->setIdxdiff($reading->getIdxdiff()); $read->setConsumption($read->getIdxdiff() * $read->getMultiplyfactor()); $read->setAmount($read->getUnitprice() * $read->getConsumption()); $this->addInvoiceReading($read); $totalAmount += $read->getAmount(); $reading->setIsBilled(1); $reading->save(); $this->setTotalamount($totalAmount); $this->checkIsFullypaid(); $this->setIsPrinted(0); $this->setIsCanceled(0); $this->save(); } public function checkIsFullypaid($receiptid=null) { $receipts = ReceiptPeer::sumReceiptsPerInvoice($this->getId()); $receiptsPaid = ReceiptPeer::sumReceiptsPaidInvoices($this->getContractId()); $invoicesPaid = InvoicePeer::sumPaidInvoices($this->getContractId()); if (($receipts >= $this->getTotalamount()) || (($receiptsPaid - $invoicesPaid) >= $this->getTotalamount())) { $this->setIsFullypaid(1); $this->setDatefullypaid(date('y-m-d')); } else { $this->setIsFullypaid(0); } } public function checkInvoicesFullypaid() { $invoices = InvoicePeer::retrieveInvoicesUnpaid($this->getContractId()); foreach ($invoices as $invoice) { $invoice->checkIsFullypaid(); // $balance = ContractPeer::contractBalance($invoice->getContractId()); // if ((-$balance) >= $invoice->getTotalamount()) // { // $invoice->setIsFullypaid(1); // } if ($invoice->getIsFullypaid()) $invoice->save(); } } }