Skip to content

Commit

Permalink
BTHAB-204: Apply contact membership type product discount to sale ord…
Browse files Browse the repository at this point in the history
…er items
  • Loading branch information
erawat committed Sep 1, 2023
1 parent 49733f1 commit 2d3e1dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1 crm-page-title>{{ ts(isUpdate ? 'Edit Quotation':'Create Quotation') }}</h1>
<div class="col-sm-5">
<input class="form-control"
ng-model="salesOrder.client_id"
ng-change="handleClientChange()"
placeholder="Client"
name="client"
crm-entityref="{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@

$scope.isUpdate = false;
$scope.formValid = true;
$scope.membeshipTypesProductDiscountPercentage = 0;
$scope.roundTo = roundTo;
$scope.formatMoney = formatMoney;
$scope.submitInProgress = false;
$scope.caseApiParam = caseApiParam;
$scope.saveQuotation = saveQuotation;
$scope.calculateSubtotal = calculateSubtotal;
$scope.currencyCodes = CurrencyCodes.getAll();
$scope.handleClientChange = handleClientChange;
$scope.handleProductChange = handleProductChange;
$scope.handleCurrencyChange = handleCurrencyChange;
$scope.salesOrderStatus = SalesOrderStatus.getAll();
Expand Down Expand Up @@ -146,7 +148,7 @@
financial_type_id: null,
unit_price: null,
quantity: null,
discounted_percentage: null,
discounted_percentage: $scope.membeshipTypesProductDiscountPercentage,
tax_rate: 0,
subtotal_amount: 0
});
Expand Down Expand Up @@ -221,6 +223,40 @@
});
}

/**
* Applies any membership type product discounts to
* each sale order line item if the selected client has any memberships.
*/
function handleClientChange () {
const clientID = $scope.salesOrder.client_id;
crmApi4('Membership', 'get', {
select: ['membership_type_id.Product_Discounts.Product_Discount_Amount'],
where: [['contact_id', '=', clientID]]
}).then(function (results) {
let discountPercentage = 0;
results.forEach((membership) => {
discountPercentage += membership['membership_type_id.Product_Discounts.Product_Discount_Amount'];
});
// make sure that the discount percentage cannot be more than 100%
if (discountPercentage > 100) {
discountPercentage = 100;
}
$scope.membeshipTypesProductDiscountPercentage = discountPercentage;
applySaleOrderItemPencentageDiscount();
CRM.alert(ts('Automatic Members Discount Applied'), ts('Product Discount'), 'success');
});
}

/**
* Applies Membership Type discounted percentage to
* each sale order item discounted percentage.
*/
function applySaleOrderItemPencentageDiscount () {
$scope.salesOrder.items.forEach((item) => {
item.discounted_percentage = item.discounted_percentage + $scope.membeshipTypesProductDiscountPercentage;
});
}

/**
* Update currency symbol if currecny field is upddated
*/
Expand Down

0 comments on commit 2d3e1dd

Please sign in to comment.