Commit 3ac52ac7 authored by Monish Deb's avatar Monish Deb
Browse files

remove CRM_Lineitemedit_Form_Add form

parent 22ceadf1
<?php
require_once 'CRM/Core/Form.php';
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_Lineitemedit_Form_Add extends CRM_Core_Form {
protected $_contributionID;
protected $_isQuickConfig;
protected $_fieldNames;
public function preProcess() {
$this->_contributionID = CRM_Utils_Request::retrieve('contribution_id', 'Positive', $this);
$this->_fieldNames = CRM_Lineitemedit_Util::getLineitemFieldNames(TRUE);
}
/**
* Set default values.
*
* @return array
*/
public function setDefaultValues() {
return array(
'line_total' => 0.00,
'tax_amount' => 0.00,
);
}
public function buildQuickForm() {
foreach ($this->_fieldNames as $fieldName) {
if ($fieldName == 'line_total') {
$this->add('text', 'line_total', ts('Total amount'), array(
'size' => 6,
'maxlength' => 14,
'readonly' => TRUE)
);
continue;
}
elseif ($fieldName == 'price_field_value_id') {
$options = array('' => '- select any price-field -') + CRM_Lineitemedit_Util::getPriceFieldLists($this->_contributionID);
$this->add('select', $fieldName, ts('Price Field'), $options, TRUE);
continue;
}
$properties = array(
'entity' => 'LineItem',
'name' => $fieldName,
'context' => 'add',
'action' => 'create',
);
if ($fieldName == 'financial_type_id') {
CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes);
$properties['options'] = $financialTypes;
}
elseif ($fieldName == 'tax_amount') {
$properties['readonly'] = TRUE;
}
$this->addField($fieldName, $properties, TRUE);
}
$this->assign('fieldNames', $this->_fieldNames);
$this->assign('currency', CRM_Core_DAO::getFieldValue(
'CRM_Financial_DAO_Currency',
CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionID, 'currency'),
'symbol',
'name'
));
$this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));
$this->addFormRule(array(__CLASS__, 'formRule'), $this);
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Add Item'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Close'),
),
));
parent::buildQuickForm();
}
public static function formRule($fields, $files, $self) {
$errors = array();
$canChangeQuantity = CRM_Lineitemedit_Util::isPriceFieldSupportQtyChange($fields['price_field_value_id']);
if ($fields['qty'] == 0) {
$errors['qty'] = ts('Line quantity cannot be zero');
}
if ($canChangeQuantity) {
$unitPrice = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $fields['price_field_value_id'], 'amount');
if ($unitPrice != $fields['unit_price']) {
$errors['unit_price'] = ts('You cannot change unit price of this text price field.');
}
}
else {
if (!CRM_Utils_Rule::integer($fields['qty'])) {
$errors['qty'] = ts('Please enter a whole number quantity.');
}
}
return $errors;
}
public function postProcess() {
$params = $this->exportValues();
$this->submit($params);
}
public function submit($values) {
// check for any cancelled line item which was recorded for same price field,
// if found then use its ID update it rather then creating a new line item as
// civicrm doesn't allow multiple line item registered against same
// contribution and price field ID
$previousLineItem = civicrm_api3('LineItem', 'get', array(
'contribution_id' => $this->_contributionID,
'price_field_value_id' => $values['price_field_value_id'],
));
$entityId = NULL;
if (!empty($previousLineItem['values'])) {
$entityId = $previousLineItem['values'][$previousLineItem['id']]['entity_id'];
}
list($entityTable, $entityID) = CRM_Lineitemedit_Util::addEntity(
$values['price_field_value_id'],
$this->_contributionID,
$values['qty'],
$entityId
);
$newLineItemParams = array(
'entity_table' => $entityTable,
'entity_id' => $entityID,
'contribution_id' => $this->_contributionID,
'price_field_id' => CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $values['price_field_value_id'], 'price_field_id'),
'label' => $values['label'],
'qty' => $values['qty'],
'unit_price' => CRM_Utils_Rule::cleanMoney($values['unit_price']),
'line_total' => CRM_Utils_Rule::cleanMoney($values['line_total']),
'price_field_value_id' => $values['price_field_value_id'],
'financial_type_id' => $values['financial_type_id'],
);
if (!empty($previousLineItem['id'])) {
$newLineItemParams['id'] = $previousLineItem['id'];
if ($entityTable == 'civicrm_participant') {
$newLineItemParams['label'] = $previousLineItem['label'];
}
}
$newLineItem = civicrm_api3('LineItem', 'create', $newLineItemParams);
// calculate balance, tax and paidamount later used to adjust transaction
$updatedAmount = CRM_Price_BAO_LineItem::getLineTotal($this->_contributionID);
$taxAmount = CRM_Lineitemedit_Util::getTaxAmountTotalFromContributionID($this->_contributionID);
// Record adjusted amount by updating contribution info and create necessary financial trxns
$trxn = CRM_Lineitemedit_Util::recordAdjustedAmt(
$updatedAmount,
$this->_contributionID,
$taxAmount
);
$lineItem = civicrm_api3('LineItem', 'getsingle', array('id' => $newLineItem['id']));
// record financial item on addition of lineitem
if ($trxn) {
CRM_Lineitemedit_Util::insertFinancialItemOnAdd($lineItem, $trxn);
}
elseif ($newLineItemParams['line_total'] == 0) {
$trxn = CRM_Lineitemedit_Util::createFinancialTrxnEntry($this->_contributionID, $newLineItemParams['line_total']);
CRM_Lineitemedit_Util::insertFinancialItemOnAdd($lineItem, $trxn);
}
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath()));
if ($entityTable == 'civicrm_membership') {
$contactId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution',
$this->_contributionID,
'contact_id'
);
$this->ajaxResponse['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact::getCountComponent('membership', $contactId);
}
}
public function testSubmit($params) {
$this->_contributionID = $params['contribution_id'];
$this->_fieldNames = CRM_Lineitemedit_Util::getLineitemFieldNames(TRUE);
$this->submit($params);
}
}
......@@ -188,7 +188,7 @@ function lineitemedit_civicrm_pre($op, $entity, $entityID, &$params) {
}
else {
if ($taxEnabled) {
$lineItemParams[$i]['tax_amount'] = CRM_Utils_Array::value($i, $params['item_tax_amount'], 0.00);
$lineItemParams[$i]['tax_amount'] = (float) CRM_Utils_Array::value($i, $params['item_tax_amount'], 0.00);
$params['tax_amount'] += $lineItemParams[$i]['tax_amount'];
}
$params['total_amount'] = $params['net_amount'] = $params['amount'] += (CRM_Utils_Array::value('line_total', $lineItemParams[$i], 0.00) + CRM_Utils_Array::value('tax_amount', $lineItemParams[$i], 0.00));
......
......@@ -96,100 +96,4 @@ class CRM_Lineitemedit_Form_AddTest extends CRM_Lineitemedit_Form_BaseTest {
}
}
public function testLineTotalChangeWithPriceSet() {
$priceFieldValues = $this->createPriceSet();
$priceFieldID = key($priceFieldValues);
$contactID = $this->createDummyContact();
$check = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$params = array(
'total_amount' => 100,
'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
'receive_date' => date('Ymd'),
'receive_date_time' => '11:27PM',
'contact_id' => $contactID,
'price_set_id' => $this->_priceSetID,
'payment_instrument_id' => $check,
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'price_' . $priceFieldID => array($priceFieldValues[$priceFieldID][0] => 1),
);
$form = new CRM_Contribute_Form_Contribution();
$form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID));
$form->testSubmit($params, CRM_Core_Action::ADD);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $contactID));
// Contribution amount and status before LineItem add
$this->assertEquals('Completed', $contribution['contribution_status']);
$this->assertEquals(100.00, $contribution['total_amount']);
$form = new CRM_Lineitemedit_Form_Add();
$lineItemInfo = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
$lineItemInfo['line_total'] = $lineItemInfo['unit_price'] += 100;
$lineItemInfo['price_field_value_id'] = $priceFieldValues[$priceFieldID][1];
$lineItemInfo['label'] = 'Price Field 2';
$form->testSubmit($lineItemInfo);
// Now select price option that has 0 amount
$form = new CRM_Lineitemedit_Form_Add();
$lineItemInfo = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'], 'options' => ['sort' => 'id DESC', 'limit' => 1]));
$lineItemInfo['line_total'] = $lineItemInfo['unit_price'] = 0;
$lineItemInfo['price_field_value_id'] = $priceFieldValues[$priceFieldID][2];
$lineItemInfo['label'] = 'Price Field 3';
$form->testSubmit($lineItemInfo);
// Contribution amount and status after LineItem edit
$contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id']));
$this->assertEquals('Partially paid', $contribution['contribution_status']);
$this->assertEquals(300.00, $contribution['total_amount']);
$actualFinancialItemEntries = $this->getFinancialItemsByContributionID($contribution['id']);
$expectedFinancialItemEntries = array(
array(
'contact_id' => $contactID,
'description' => 'Price Field 1',
'amount' => 100.00,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid'),
),
array(
'contact_id' => $contactID,
'description' => 'Price Field 2',
'amount' => 200.00,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid'),
),
array(
'contact_id' => $contactID,
'description' => 'Price Field 3',
'amount' => 0.00,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid'),
),
);
$this->checkArrayEqualsByAttributes($expectedFinancialItemEntries, $actualFinancialItemEntries);
$actualFinancialTrxnEntries = $this->getFinancialTrxnsByContributionID($contribution['id']);
$expectedFinancialTrxnEntries = array(
array(
'total_amount' => 100.00,
'net_amount' => 100.00,
'is_payment' => 1,
'payment_instrument_id' => $check,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
),
array(
'total_amount' => 200.00,
'net_amount' => 200.00,
'is_payment' => 0,
'payment_instrument_id' => $check,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
),
array(
'total_amount' => 0.00,
'net_amount' => 0.00,
'is_payment' => 0,
'payment_instrument_id' => $check,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
),
);
$this->checkArrayEqualsByAttributes($expectedFinancialTrxnEntries, $actualFinancialTrxnEntries);
}
}
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . '../BaseTest.php';
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
/**
* FIXME - Add test description.
*
* Tips:
* - With HookInterface, you may implement CiviCRM hooks directly in the test class.
* Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar).
* - With TransactionalInterface, any data changes made by setUp() or test****() functions will
* rollback automatically -- as long as you don't manipulate schema or truncate tables.
* If this test needs to manipulate schema or truncate tables, then either:
* a. Do all that using setupHeadless() and Civi\Test.
* b. Disable TransactionalInterface, and handle all setup/teardown yourself.
*
* @group headless
*/
class CRM_Lineitemedit_Form_SaleTax_AddTest extends CRM_Lineitemedit_Form_BaseTest {
public function setUpHeadless() {
// Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
// See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
return \Civi\Test::headless()
->installMe(__DIR__)
->apply();
}
public function setUp() {
$this->_createContri = FALSE;
parent::setUp();
}
public function tearDown() {
$this->disableTaxAndInvoicing();
parent::tearDown();
}
public function testLineTotalChangeWithPriceSet() {
$contactId = $this->createDummyContact();
$this->enableTaxAndInvoicing();
$FTname = 'Financial-Type -' . substr(sha1(rand()), 0, 7);
$financialType = $this->createFinancialType(array('name' => $FTname));
$financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
$priceFieldValues = $this->createPriceSet(array('financial_type_id' => $financialType['id']));
$priceFieldID = key($priceFieldValues);
$contactID = $this->createDummyContact();
$check = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
$params = array(
'total_amount' => 110,
'fee_amount' => 0.00,
'net_amount' => 110,
'tax_amount' => 10,
'financial_type_id' => $financialType['id'],
'receive_date' => '04/21/2015',
'receive_date_time' => '11:27PM',
'contact_id' => $contactID,
'price_set_id' => $this->_priceSetID,
'payment_instrument_id' => $check,
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'price_' . $priceFieldID => array($priceFieldValues[$priceFieldID][0] => 1),
);
$form = new CRM_Contribute_Form_Contribution();
$form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($this->_priceSetID));
$form->testSubmit($params, CRM_Core_Action::ADD);
$contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $contactID));
// Contribution amount and status before LineItem add
$this->assertEquals('Completed', $contribution['contribution_status']);
$this->assertEquals(110.00, $contribution['total_amount']);
$form = new CRM_Lineitemedit_Form_Add();
$lineItemInfo = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
$lineItemInfo['line_total'] = $lineItemInfo['unit_price'] += 100;
$lineItemInfo['tax_amount'] += 10;
$lineItemInfo['price_field_value_id'] = $priceFieldValues[$priceFieldID][1];
$form->testSubmit($lineItemInfo);
// Contribution amount and status after LineItem edit
$contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id']));
$this->assertEquals('Partially paid', $contribution['contribution_status']);
$this->assertEquals(330.00, $contribution['total_amount']);
$financialAccountID = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialType['id'], 'Income Account is');
$actualFinancialItemEntries = $this->getFinancialItemsByContributionID($contribution['id']);
$expectedFinancialItemEntries = array(
array(
'contact_id' => $contactID,
'description' => 'Price Field 1',
'amount' => 100.00,
'financial_account_id' => $financialAccountID,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid'),
),
array(
'contact_id' => $contactID,
'description' => 'Sales Tax',
'amount' => 10.00,
'financial_account_id' => CRM_Lineitemedit_Util::getFinancialAccountId($financialType['id']),
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Paid'),
),
array(
'contact_id' => $contactID,
'description' => 'Price Field 1',
'amount' => 200.00,
'financial_account_id' => $financialAccountID,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid'),
),
array(
'contact_id' => $contactID,
'description' => 'Sales Tax',
'amount' => 20.00,
'financial_account_id' => CRM_Lineitemedit_Util::getFinancialAccountId($financialType['id']),
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialItem', 'status_id', 'Unpaid'),
),
);
$this->checkArrayEqualsByAttributes($expectedFinancialItemEntries, $actualFinancialItemEntries);
$actualFinancialTrxnEntries = $this->getFinancialTrxnsByContributionID($contribution['id']);
$expectedFinancialTrxnEntries = array(
array(
'total_amount' => 110.00,
'net_amount' => 110.00,
'is_payment' => 1,
'payment_instrument_id' => $check,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
),
array(
'total_amount' => 220.00,
'net_amount' => 220.00,
'is_payment' => 0,
'payment_instrument_id' => $check,
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
),
);
$this->checkArrayEqualsByAttributes($expectedFinancialTrxnEntries, $actualFinancialTrxnEntries);
}
/**
* Function to create contribution with tax.
*/
public function createContributionWithTax() {
$contactId = $this->createDummyContact();
$this->enableTaxAndInvoicing();
$financialType = $this->createFinancialType();
$financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
$form = new CRM_Contribute_Form_Contribution();
$form->testSubmit(array(
'total_amount' => 100,
'financial_type_id' => $financialType['id'],
'receive_date' => '04/21/2015',
'receive_date_time' => '11:27PM',
'contact_id' => $contactId,
'contribution_status_id' => 1,
'price_set_id' => 0,
),
CRM_Core_Action::ADD
);
$contribution = $this->callAPISuccessGetSingle('Contribution',
array(
'contact_id' => $contactId,
'return' => array('tax_amount', 'total_amount'),
)
);
return array($contribution, $financialAccount);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment