public function saveDebitorAccount($recordData) { $obj = new Billing_Model_DebitorAccount(); $obj->setFromArray($recordData); try { $objId = $obj->getId(); $obj = Billing_Controller_DebitorAccount::getInstance()->get($objId); $obj->setFromArray($recordData); $obj = Billing_Controller_DebitorAccount::getInstance()->update($obj); } catch (Exception $e) { //echo $e->__toString(); $obj = Billing_Controller_DebitorAccount::getInstance()->create($obj); } return $obj->toArray(); }
public function prepareMonitionExport(Billing_Model_BatchJob $job) { error_reporting(E_ALL); set_time_limit(0); \org\sopen\dev\DebugLogger::openLogFileOverwrite(CSopen::instance()->getCustomerPath() . '/conf/logs/mon.log'); //throw new Exception(CSopen::instance()->getCustomerPath().'/conf/logs/mon.log'); try { $db = Tinebase_Core::getDb(); $tm = Tinebase_TransactionManager::getInstance(); $tId = $tm->startTransaction($db); $job = Billing_Api_BatchJobManager::getInstance()->loadBatchJob($job->getId()); // Billing_Api_BatchJobManager::getInstance()->startBatchJob(); $data = $job->getData(); $filters = $data['filters']; $aContexts = $data['contexts']; if (!is_array($aContexts)) { $aContexts = Zend_Json::decode($aContexts); } $pagination = new Tinebase_Model_Pagination(); //require_once 'Payment/Monition.php'; if (!is_array($filters)) { $filters = Zend_Json::decode($filters); } /*if(!is_array($paymentTypeKeys)){ $paymentTypeKeys = Zend_Json::decode($paymentTypeKeys); } $paymentTypeKeys = array( 1 => 'DEBIT', 2 => 'DEBIT_GM' );*/ $monitionStage1Days = (int) Tinebase_Core::getPreference('Billing')->getValue(Billing_Preference::MONITION_STAGE1); // get all open items (for debit) // grouped by debitors $filters[] = array('field' => 'due_days', 'operator' => 'greater', 'value' => $monitionStage1Days); /* $filters[] = array( 'field' => 'type', 'operator' => 'notin', 'value' => $paymentTypeKeys );*/ $filters[] = array('field' => 'state', 'operator' => 'not', 'value' => 'DONE'); $contextFilters = array(); foreach ($aContexts as $context => $flag) { if ($flag) { $contextFilters[] = $context; } } $filters[] = array('field' => 'erp_context_id', 'operator' => 'in', 'value' => $contextFilters); //$rawFilters = $filters; //$paymentTypeCount = count($paymentTypeKeys); /*$filters[] = array( 'field' => 'payment_method_id', 'operator' => 'equals', 'value' => $paymentTypeKeys[1] );*/ //print_r($filters); $filter = new Billing_Model_OpenItemFilter($filters, 'AND'); // count membership matching filters $openItemIds = Billing_Controller_OpenItem::getInstance()->search($filter, new Tinebase_Model_Pagination(array('sort' => 'id', 'dir' => 'ASC')), false, true); //print_r($openItemIds); $debitorOpenItemIds = array(); foreach ($openItemIds as $openItemId) { $openItem = Billing_Controller_OpenItem::getInstance()->get($openItemId); $debitorId = $openItem->getForeignId('debitor_id'); if (!array_key_exists($debitorId, $debitorOpenItemIds)) { $debitorOpenItemIds[$debitorId] = array(); } $debitorOpenItemIds[$debitorId][] = $openItemId; } foreach ($debitorOpenItemIds as $debitorId => $debitorOpenItems) { $debitor = Billing_Controller_Debitor::getInstance()->get($debitorId); $contact = $debitor->getForeignRecord('contact_id', Addressbook_Controller_Contact::getInstance()); $aSaldation = Billing_Controller_DebitorAccount::getInstance()->getSummationByDebitorId($debitorId, $contextFilters); $saldation = $aSaldation['sum']; $batchJobMonition = new Billing_Model_BatchJobMonition(null, true); $batchJobMonition->setFromArray(array('job_id' => $job->getId(), 'contact_id' => $contact->getId(), 'debitor_id' => $debitor->getId(), 'monition_stage' => 1, 'total_sum' => 0, 'open_sum' => 0, 'total_saldation' => 0, 'count_pos' => 0, 'monition_lock' => $debitor->__get('is_monition_locked'), 'skip' => false, 'action_text' => '', 'action_data' => null, 'action_type' => 'DRYRUN', 'action_state' => 'OPEN', 'error_info' => $errorInfo, 'created_datetime' => Zend_Date::now(), 'valid_datetime' => Zend_Date::now(), 'to_process_datetime' => Zend_Date::now(), 'process_datetime' => Zend_Date::now(), 'usage' => '')); $batchJobMonition = $this->create($batchJobMonition); $batchJobMonitionId = $batchJobMonition->getId(); $posCount = count($debitorOpenItems); $totalSum = 0; $totalBruttoSum = 0; $usages = array(); $maxMonitionStage = 1; foreach ($debitorOpenItems as $openItemId) { $openItem = Billing_Controller_OpenItem::getInstance()->get($openItemId); $sum = (double) $openItem->__get('open_sum'); $totalBrutto = (double) $openItem->__get('total_brutto'); $batchJobMonitionItem = new Billing_Model_BatchJobMonitionItem(null, true); $batchJobMonitionItem->setFromArray(array('batch_monition_id' => $batchJobMonitionId, 'open_item_id' => $openItem->getId(), 'erp_context_id' => $openItem->__get('erp_context_id'), 'open_sum' => $sum, 'total_sum' => $totalBrutto, 'monition_stage' => $openItem->getNextMonitionStage(), 'due_days' => $openItem->__get('due_days'), 'skip' => false, 'usage' => $openItem->__get('usage'))); $usages[] = $openItem->__get('usage'); $totalSum += $sum; $totalBruttoSum += $totalBrutto; $maxMonitionStage = max($maxMonitionStage, $openItem->getNextMonitionStage()); Billing_Controller_BatchJobMonitionItem::getInstance()->create($batchJobMonitionItem); } $usage = implode(' ', $usages); $batchJobMonition->__set('usage', $usage); $batchJobMonition->__set('open_sum', $totalSum); $batchJobMonition->__set('total_sum', $totalBruttoSum); $batchJobMonition->__set('total_saldation', $saldation); $batchJobMonition->__set('monition_stage', $maxMonitionStage); $batchJobMonition->__set('count_pos', $posCount); $this->update($batchJobMonition); } Billing_Api_BatchJobManager::getInstance()->finish(); $tm->commitTransaction($tId); \org\sopen\dev\DebugLogger::close(); return array('state' => 'success', 'result' => null); } catch (Exception $e) { // $e->__toString(); $tm->rollback($tId); Billing_Api_BatchJobManager::getInstance()->finishError($e->__toString()); \org\sopen\dev\DebugLogger::log($e->__toString); \org\sopen\dev\DebugLogger::close(); return array('state' => 'failure', 'result' => null, 'errorInfo' => array('message' => $e->getMessage(), 'trace' => $e->getTrace())); } }
public function onPaymentBooked($payment, $booking) { if ($payment->__get('debitor_id')) { $debitorAccount = Billing_Controller_DebitorAccount::getInstance()->getEmptyDebitorAccount(); $debitor = $payment->getForeignRecord('debitor_id', Billing_Controller_Debitor::getInstance()); $debitorAccount->__set('debitor_id', $debitor->getId()); $debitorAccount->__set('create_date', new Zend_Date($payment->__get('payment_date'))); $debitorAccount->__set('value_date', new Zend_Date($payment->__get('payment_date'))); $debitorAccount->__set('usage', $payment->__get('usage')); $debitorAccount->__set('erp_context_id', $payment->__get('erp_context_id')); $debitorAccount->__set('payment_id', $payment->__get('id')); $debitorAccount->__set('booking_id', $booking->getId()); $debitorAccount->__set('receipt_id', $payment->getForeignId('receipt_id')); // $debitorAccount->__set('open_item_id', $payment->getForeignId('open_item_id')); $debitorAccount->__set('donation_id', $payment->getForeignId('donation_id')); // other object: like donation for example (not a clean approach) $debitorAccount->__set('object_id', $payment->getForeignId('object_id')); $debitorAccount->__set('donation_id', $payment->getForeignId('donation_id')); $debitorAccount->__set('is_cancelled', $payment->__get('is_cancelled')); $debitorAccount->__set('is_cancellation', $payment->__get('is_cancellation')); //$cKto = $payment->getForeignId('account_system_id_haben'); //$dKto = $payment->getForeignId('account_system_id'); switch ($payment->__get('type')) { case 'DEBIT': $debitorAccount->__set('item_type', 'DEBIT'); $debitorAccount->__set('h_brutto', abs($payment->getAmount(false))); break; case 'CREDIT': $debitorAccount->__set('item_type', 'CREDIT'); $debitorAccount->__set('s_brutto', abs($payment->getAmount(false))); break; } if ($payment->__get('payment_type') == 'PAYMENT') { $debitorAccount->__set('item_type', 'PAYMENT'); } else { $debitorAccount->__set('item_type', 'DISAGIO'); } Billing_Controller_DebitorAccount::getInstance()->create($debitorAccount); } /* if($payment->__get('receipt_id')){ $receipt = $payment->getForeignRecord('receipt_id', Billing_Controller_Receipt::getInstance()); $openItem = Billing_Controller_OpenItem::getInstance()->getByReceiptId($receipt->getId()); Billing_Controller_OpenItem::getInstance()->payOpenItem($openItem->getId(), $payment); $totalBrutto = $receipt->__get('total_brutto'); $amount = $payment->__get('amount'); if($payment->__get('amount') == $totalBrutto){ $receipt->__set('payment_state', 'PAYED'); }elseif($payment->__get('amount') == 0){ $receipt->__set('payment_state', 'TOBEPAYED'); }elseif($payment->__get('amount') < $totalBrutto){ $receipt->__set('payment_state', 'PARTLYPAYED'); } $receipt = Billing_Controller_Receipt::getInstance()->update($receipt); }*/ }
/** * event handler function * * all events get routed through this function * * @param Tinebase_Event_Abstract $_eventObject the eventObject * * @todo write test */ public function handleEvents(Tinebase_Event_Abstract $_eventObject) { try { if ($_eventObject instanceof Billing_Events_BillableReceiptCreated) { // create open item out of invoice or credit $db = Tinebase_Core::getDb(); $tm = Tinebase_TransactionManager::getInstance(); $tId = $tm->startTransaction($db); try { $receipt = Billing_Controller_Receipt::getInstance()->get($_eventObject->receipt->getId()); // set payed and open sum Billing_Controller_Receipt::getInstance()->onBillableReceiptCreated($receipt); // create according open item Billing_Controller_OpenItem::getInstance()->onBillableReceiptCreated($receipt); if (Tinebase_Core::getPreference('Billing')->getValue(Billing_Preference::FIBU_INSTANT_BOOK_BILLABLE)) { // do fibu booking -> fire event: BillableReceiptBooked Billing_Controller_Booking::getInstance()->onBillableReceiptCreated($receipt); } $tm->commitTransaction($tId); } catch (Exception $e) { $tm->rollback($tId); throw $e; } } if ($_eventObject instanceof Billing_Events_PaymentReverted) { // TODO: NRW //Billing_Controller_OpenItem::getInstance()->onOpenItemPayed($_eventObject->payment); } if ($_eventObject instanceof Billing_Events_PaymentCreated) { // TODO: NRW try { $payment = $_eventObject->payment; Billing_Controller_Booking::getInstance()->onPaymentCreated($payment); if ($payment->__get('receipt_id')) { $receipt = $payment->getForeignRecord('receipt_id', Billing_Controller_Receipt::getInstance()); $openItem = Billing_Controller_OpenItem::getInstance()->getByReceiptId($receipt->getId()); Billing_Controller_OpenItem::getInstance()->payOpenItem($openItem->getId(), $payment); } if ($payment->isReturnDebit()) { Tinebase_Event::fireEvent(new Billing_Events_PaymentDebitReturnCreated($payment)); } } catch (Exception $e) { echo $e->__toString(); } //Billing_Controller_OpenItem::getInstance()->onOpenItemPayed($_eventObject->payment); } if ($_eventObject instanceof Billing_Events_PaymentDebitReturnCreated) { // TODO: NRW try { $payment = $_eventObject->payment; Billing_Controller_OpenItem::getInstance()->onPaymentDebitReturnCreated($payment); } catch (Exception $e) { echo $e->__toString(); } //Billing_Controller_OpenItem::getInstance()->onOpenItemPayed($_eventObject->payment); } if ($_eventObject instanceof Billing_Events_PaymentBooked) { // TODO: NRW try { $payment = $_eventObject->payment; $booking = $_eventObject->booking; Billing_Controller_DebitorAccount::getInstance()->onPaymentBooked($payment, $booking); } catch (Exception $e) { echo $e->__toString(); } } if ($_eventObject instanceof Billing_Events_PaymentDonationDetected) { // TODO: NRW try { $payment = $_eventObject->payment; Billing_Controller_DebitorAccount::getInstance()->onPaymentDonationDetected($payment); } catch (Exception $e) { echo $e->__toString(); } } if ($_eventObject instanceof Billing_Events_BillableReceiptBooked) { try { // TODO: NRW $receipt = Billing_Controller_Receipt::getInstance()->get($_eventObject->receipt->getId()); $openItem = $_eventObject->openItem; $booking = $_eventObject->booking; // after booking -> debitor account gets created Billing_Controller_DebitorAccount::getInstance()->onBillableReceiptBooked($receipt, $openItem, $booking); $receipt->__set('booking_id', $booking->getId()); $openItem->__set('booking_id', $booking->getId()); Billing_Controller_Receipt::getInstance()->update($receipt); Billing_Controller_OpenItem::getInstance()->update($openItem); } catch (Exception $e) { echo $e->__toString(); } } if ($_eventObject instanceof Billing_Events_BillableReceiptReverted) { try { // TODO: NRW $receipt = $_eventObject->revertedReceipt; $reversionReceipt = $_eventObject->reversionReceipt; // after booking -> debitor account gets created Billing_Controller_DebitorAccount::getInstance()->onBillableReceiptReverted($receipt, $reversionReceipt); } catch (Exception $e) { echo $e->__toString(); } } } catch (Exception $e) { Tinebase_Core::getLogger()->warn($e->__toString()); echo $e->__toString(); } }
public function prepareDebitDtaExport(Billing_Model_BatchJob $job) { error_reporting(E_ALL); set_time_limit(0); try { $dueDate = new Zend_Date(); $db = Tinebase_Core::getDb(); $tm = Tinebase_TransactionManager::getInstance(); $tId = $tm->startTransaction($db); $job = Billing_Api_BatchJobManager::getInstance()->loadBatchJob($job->getId()); // Billing_Api_BatchJobManager::getInstance()->startBatchJob(); $data = $job->getData(); $filters = $data['filters']; $aContexts = $data['contexts']; if (!is_array($aContexts)) { $aContexts = Zend_Json::decode($aContexts); } $pagination = new Tinebase_Model_Pagination(); //require_once 'Payment/DTA.php'; if (!is_array($filters)) { $filters = Zend_Json::decode($filters); } // get all open items (for debit) // grouped by debitors /*$filters[] = array( 'field' => 'banking_exp_date', 'operator' => 'isnull', 'value' => '' );*/ $filters[] = array('field' => 'state', 'operator' => 'not', 'value' => 'DONE'); $filters[] = array('field' => 'type', 'operator' => 'equals', 'value' => 'DEBIT'); $filters[] = array('field' => 'is_cancelled', 'operator' => 'equals', 'value' => 0); $filters[] = array('field' => 'is_cancellation', 'operator' => 'equals', 'value' => 0); //$contextFilters = array('ERP'); $contextFilters = array(); foreach ($aContexts as $context => $flag) { if ($flag) { $contextFilters[] = $context; } } $filters[] = array('field' => 'erp_context_id', 'operator' => 'in', 'value' => $contextFilters); $rawFilters = $filters; $paymentTypeCount = count($paymentTypeKeys); $filter = new Billing_Model_OpenItemFilter($filters, 'AND'); // due date: means date of execution of debit -> necessary for determination of sepa mandates // Telefonat: Rudat, hhartl 14.05.2014 // "wenn im Filter angegeben, dann verwenden, ansonsten Tagesdatum" if ($filter->isFilterSet("due_date")) { $dueDate = new Zend_Date($filter->getFilter("due_date")->getValue()); } // count membership matching filters $openItemIds = Billing_Controller_OpenItem::getInstance()->search($filter, new Tinebase_Model_Pagination(array('sort' => 'id', 'dir' => 'ASC')), false, true); //ob_start(); $debitorOpenItemIds = array(); $limit = 0; //echo "global count: ".count($openItemIds); foreach ($openItemIds as $openItemId) { /*if($limit++ > 250){ break; }*/ $openItem = Billing_Controller_OpenItem::getInstance()->get($openItemId); $debitorId = $openItem->getForeignId('debitor_id'); if (!array_key_exists($debitorId, $debitorOpenItemIds)) { $debitorOpenItemIds[$debitorId] = array(); } $debitorOpenItemIds[$debitorId][] = $openItemId; } $addFilters = array(); $addFilters[] = array('field' => 'membership_type', 'operator' => 'notin', 'value' => array('MEMBER_EX', 'MEMBER_GESCHENK', 'SCHENKER')); foreach ($debitorOpenItemIds as $debitorId => $debitorOpenItems) { $debitor = null; $contact = null; $contactId = null; $isError = false; try { $debitor = Billing_Controller_Debitor::getInstance()->get($debitorId); $contactId = $debitor->getForeignId('contact_id'); $contact = $debitor->getForeignRecord('contact_id', Addressbook_Controller_Contact::getInstance()); $valid = 'UNKNOWN'; $activeMembershipIds = Membership_Controller_SoMember::getInstance()->getActiveMembershipsByContactId($contact->getId(), $dueDate, $addFilters); $mandate = null; $count = count($activeMembershipIds); if (count($activeMembershipIds) == 1) { $membership = Membership_Controller_SoMember::getInstance()->getSoMember($activeMembershipIds[0]); if ($membership->allowsSepaDirectDebit($dueDate)) { //echo "has::"; $hasMandate = true; $mandate = $membership->getForeignRecordBreakNull('sepa_mandate_id', Billing_Controller_SepaMandate::getInstance()); $valid = 'YES'; } $bankAccount = $membership->getForeignRecordBreakNull('bank_account_id', Billing_Controller_BankAccount::getInstance()); } elseif (count($activeMembershipIds) == 0) { $regularDonations = Donator_Controller_RegularDonation::getInstance()->getByContactId($contact->getId()); foreach ($regularDonations as $regDon) { if ($regDon->allowsSepaDirectDebit()) { $mandate = $regDon->getForeignRecordBreakNull('sepa_mandate_id', Billing_Controller_SepaMandate::getInstance()); $valid = 'YES'; //echo "has::"; break; } } } $bankAccountName = ''; $bankName = ''; //echo "adr $contactId - count: $count\r\n"; if (is_null($mandate)) { $bankAccount = null; $valid = 'NO'; $sepaMandateId = null; $bankAccountId = null; $receipt = $openItem->getForeignRecordBreakNull('receipt_id', Billing_Controller_Receipt::getInstance()); $text = 'Kein gültiges Sepa-Mandat'; if ($receipt->getForeignId('payment_method_id') != 'DEBIT') { continue; } } else { $text = 'Sepa-Lastschrift'; $bankAccountId = $mandate->getForeignIdBreakNull('bank_account_id'); $sepaMandateId = $mandate->getId(); } $errorInfo = ''; // REMARK[HH, 2013-08-23]: now set saldation contexts chosen by checkbox in start dialog: // fixes quickhack hardcoded array('MEMBERSHIP','DONATOR') // $aSaldation = Billing_Controller_DebitorAccount::getInstance()->getSummationByDebitorId($debitorId, $contextFilters); $saldation = $aSaldation['sum']; $actionState = 'OPEN'; } catch (Exception $e) { $isError = true; $valid = "NO"; $text = "Fehler - Debitor {$debitorId} kann nicht verarbeitet werden."; $em = ""; if (is_null($debitor)) { $em .= "Debitor konnte nicht geladen werden " . $debitorId . " "; } if (is_null($contact) || is_null($contactId)) { $em .= "Kontakt konnte nicht geladen werden. Debitor.Nr.:" . $debitorId; } $errorInfo = "Fehler: " . $em . " <br/>\r\n Original-Meldung: " . $e->__toString(); $bankAccountId = null; $mandate = null; $actionState = 'ERROR'; $bankAccountName = ""; $bankName = ""; $sepaMandateId = null; $contactId = $debitorId; } $batchJobDta = new Billing_Model_BatchJobDta(null, true); $batchJobDta->setFromArray(array('job_id' => $job->getId(), 'contact_id' => $contactId, 'debitor_id' => $debitorId, 'bank_account_id' => $bankAccountId, 'bank_account_usage_id' => 1, 'sepa_mandate_id' => $sepaMandateId, 'bank_valid' => $valid, 'bank_account_number' => 0, 'bank_code' => 0, 'bank_account_name' => $bankAccountName, 'bank_name' => $bankName, 'total_sum' => 0, 'total_saldation' => $saldation, 'diff_saldation' => 0, 'count_pos' => 0, 'skip' => !is_null($mandate), 'action_text' => $text, 'action_data' => null, 'action_type' => 'DRYRUN', 'action_state' => $actionState, 'error_info' => $errorInfo, 'created_datetime' => Zend_Date::now(), 'valid_datetime' => Zend_Date::now(), 'to_process_datetime' => Zend_Date::now(), 'process_datetime' => Zend_Date::now(), 'usage' => '')); $batchJobDta = $this->create($batchJobDta); if (!$isError) { $batchJobDtaId = $batchJobDta->getId(); $posCount = count($debitorOpenItems); $totalSum = 0; $usages = array(); foreach ($debitorOpenItems as $openItemId) { $openItem = Billing_Controller_OpenItem::getInstance()->get($openItemId); $sum = (double) $openItem->__get('open_sum'); $batchJobDtaItem = new Billing_Model_BatchJobDtaItem(null, true); $batchJobDtaItem->setFromArray(array('batch_dta_id' => $batchJobDtaId, 'open_item_id' => $openItem->getId(), 'erp_context_id' => $openItem->__get('erp_context_id'), 'total_sum' => $sum, 'skip' => false, 'usage' => $openItem->__get('usage'))); $usages[] = $openItem->__get('usage'); $totalSum += $sum; Billing_Controller_BatchJobDtaItem::getInstance()->create($batchJobDtaItem); } $usage = implode(' ', $usages); $batchJobDta->__set('usage', $usage); $batchJobDta->__set('total_sum', $totalSum); $batchJobDta->__set('diff_saldation', $totalSum + $saldation); if ($totalSum + $saldation != 0) { $batchJobDta->__set('action_text', $batchJobDta->__get('action_text') . ' Differenz überprüfen'); } $batchJobDta->__set('count_pos', $posCount); $this->update($batchJobDta); } } //$log = ob_get_clean(); //Billing_Api_BatchJobManager::getInstance()->finishError($log); Billing_Api_BatchJobManager::getInstance()->finish(); $tm->commitTransaction($tId); return array('state' => 'success', 'result' => null); } catch (Exception $e) { echo $e->__toString(); $tm->rollback($tId); Billing_Api_BatchJobManager::getInstance()->finishError($e->__toString()); return array('state' => 'failure', 'result' => null, 'errorInfo' => array('message' => $e->getMessage(), 'trace' => $e->getTrace())); } }