/** * * Completely reverse an invoice by creating a credit receipt * @param string $receiptId */ public function reverseInvoice($receiptId, $orderPositionData) { \org\sopen\dev\DebugLogger::openLogFileOverwrite(CSopen::instance()->getCustomerPath() . '/conf/logs/revinvoice.log'); if (!is_array($orderPositionData)) { $orderPositionData = Zend_Json::decode($orderPositionData); } $rController = Billing_Controller_Receipt::getInstance(); $receipt = $rController->get($receiptId); if ($receipt->__get('receipt_state') !== 'VALID') { throw new Billing_Exception_Receipt('Receipt cannot be reverted twice'); } $order = $receipt->getForeignRecord('order_id', $this); $rpController = Billing_Controller_ReceiptPosition::getInstance(); $opController = Billing_Controller_OrderPosition::getInstance(); $receiptPositions = $rpController->getByReceiptId($receiptId); $creditReceipt = Billing_Model_Receipt::createCredit(); $creditReceipt->__set('order_id', $order->getId()); $creditReceipt->__set('invoice_nr', $receipt->__get('invoice_nr')); $creditReceipt->__set('credit_date', $receipt->__get('invoice_date')); $creditReceipt->__set('invoice_date', $receipt->__get('invoice_date')); // take same payment method for credit receipt $creditReceipt->__set('payment_method_id', $receipt->getForeignId('payment_method_id')); $creditReceipt->__set('is_cancellation', true); $receipt->__set('is_cancelled', true); \org\sopen\dev\DebugLogger::log('RECHNUNG ' . print_r($receipt, true)); $creditReceipt = $rController->create($creditReceipt); $creditReceipt = $rController->get($creditReceipt->getId()); $countPos = count($receiptPositions); $creditPositions = array(); $opCount = count($orderPositionData); $aOrderPosData = null; if (is_array($orderPositionData)) { $aOrderPosData = org\sopen\app\util\arrays\ArrayHelper::getIndexedWithPropertyFromList($orderPositionData, 'id'); } $partReverse = false; foreach ($receiptPositions as $invoicePosition) { //print_r($invoicePosition); if (is_array($aOrderPosData) && $opCount > 0 && (!array_key_exists($invoicePosition->getId(), $aOrderPosData) || !$aOrderPosData[$invoicePosition->getId()]['invert_amount'])) { $partReverse = true; continue; } $creditPosition = clone $invoicePosition; $creditPosition->__set('id', null); $creditPosition->__set('position_nr', ++$countPos); $creditPosition->__set('name', $creditPosition->__get('name') . ' -STORNO'); if (is_array($aOrderPosData) && array_key_exists($invoicePosition->getId(), $aOrderPosData) && $aOrderPosData[$invoicePosition->getId()]['invert_amount']) { $creditPosition->__set('amount', $aOrderPosData[$invoicePosition->getId()]['invert_amount']); } $opController->invert($creditPosition); $creditPosition->flatten(); $creditPositions[] = $opController->create($creditPosition); } $rpController->addOrderPositions($creditReceipt, $creditPositions); $creditReceipt = Billing_Controller_Receipt::getInstance()->get($creditReceipt->getId()); /* <value>REVERTED</value> <value>PARTLYREVERTED</value> <value>ISREVERSION</value> <value>ISPARTREVERSION</value> */ $receipt->__set('receipt_state', 'REVERTED'); $creditReceipt->__set('receipt_state', 'ISREVERSION'); $receipt->__set('revision_receipt_id', $creditReceipt->getId()); $creditReceipt->__set('revision_receipt_id', $receipt->getId()); //$creditReceipt->__set('payment_state', 'PAYED'); $creditReceipt->__set('open_sum', -1 * (double) $receipt->__get('open_sum')); //$creditReceipt->__set('payed_sum',$creditReceipt->__get('total_brutto')); $creditReceipt->payTotal(); if ($partReverse) { $receipt->__set('payment_state', 'PARTLYPAYED'); $receipt->__set('receipt_state', 'PARTLYREVERTED'); $creditReceipt->__set('receipt_state', 'ISPARTREVERSION'); $receipt->payValue($creditReceipt->__get('total_brutto')); } else { try { $receipt->payTotal(); } catch (Exception $e) { // @todo: silent failure: check OK } } $creditReceipt->__set('donation_id', $receipt->getForeignId('donation_id')); $creditReceipt->__set('reversion_record_id', $receipt->getId()); $creditReceipt->__set('revision_receipt_id', $receipt->getId()); $receipt->__set('reversion_record_id', $creditReceipt->getId()); $receipt->__set('revision_receipt_id', $creditReceipt->getId()); $receipt = $rController->update($receipt); $tags = $receipt->__get('tags'); if ($tags) { $creditReceipt->__set('tags', $tags->toArray()); } $creditReceipt = $rController->update($creditReceipt); \org\sopen\dev\DebugLogger::log('RECHNUNG_update ' . print_r($receipt, true)); \org\sopen\dev\DebugLogger::log('GUTSCHRIFT ' . print_r($creditReceipt, true)); Tinebase_Event::fireEvent(new Billing_Events_BillableReceiptCreated($creditReceipt)); Tinebase_Event::fireEvent(new Billing_Events_BillableReceiptReverted($receipt, $creditReceipt)); \org\sopen\dev\DebugLogger::close(); return $creditReceipt; }
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())); } }
private static function doSepa($contact) { try { $memberships = Membership_Controller_SoMember::getInstance()->getByContactId($contact->getId()); foreach ($memberships as $membership) { $memKind = $membership->__get('membership_type'); if ($memKind == 'MEMBER_EX' || $memKind == 'INTERESTED') { continue; } if ($memKind == 'MEMBER_GESCHENK') { $parentMembership = $membership->getForeignRecord('parent_member_id', Membership_Controller_SoMember::getInstance()); $bankAccount = Billing_Api_BankAccount::getFromSoMember($parentMembership); } else { $bankAccount = Billing_Api_BankAccount::getFromSoMember($membership); } $objBankAccount = $bankAccount->convertSepa(); if (!is_null($objBankAccount)) { $usage = $objBankAccount->addUsageMembership($membership); $membership->__set('bank_account_id', $objBankAccount->getId()); $membership->__set('bank_account_usage_id', $usage->getId()); Membership_Controller_SoMember::getInstance()->update($membership); $paymentMethod = $bankAccount->getPaymentMethod(); if ($paymentMethod == 'DEBIT' || $paymentMethod == 'DEBIT_GM') { Billing_Controller_SepaMandate::getInstance()->generateSepaMandateForBankAccountUsage($usage); } } } $fundMasters = Donator_Controller_FundMaster::getInstance()->getAllByContactIdBreakNull($contact->getId()); if (!is_null($fundMasters)) { foreach ($fundMasters as $fundMaster) { if ($fundMaster instanceof Donator_Model_FundMaster) { //regular donations $regularDonations = Donator_Controller_RegularDonation::getInstance()->getByFundMasterId($fundMaster->getId()); foreach ($regularDonations as $regDon) { if (!$regDon->__get('terminated') && !$regDon->__get('on_hold')) { $bankAccount = Billing_Api_BankAccount::getFromRegularDonation($regDon); $objBankAccount = $bankAccount->convertSepa(); if (!is_null($objBankAccount)) { $usage = $objBankAccount->addUsageRegularDonation($regDon); $regDon->__set('bank_account_id', $objBankAccount->getId()); $regDon->__set('bank_account_usage_id', $usage->getId()); Donator_Controller_RegularDonation::getInstance()->update($regDon); $paymentMethod = $bankAccount->getPaymentMethod(); if ($paymentMethod == 'DEBIT' || $paymentMethod == 'DEBIT_GM') { Billing_Controller_SepaMandate::getInstance()->generateSepaMandateForBankAccountUsage($usage); } } } } } } } //$bankAccount = Billing_Api_BankAccount::getFromFundMaster($fundMaster); //$bankAccountCollection->attach($bankAccount); $bankAccount = Billing_Api_BankAccount::getFromContact($contact); $objBankAccount = $bankAccount->convertSepa(); if (!is_null($objBankAccount)) { $objBankAccount->addUsageAll(); } } catch (Exception $e) { \org\sopen\dev\DebugLogger::log('error ' . self::$errorCounter++ . ' contactId: ' . $contact->getId() . $e->getMessage()); return; } }