Instantly share code, notes, and snippets.

Embed
What would you like to do?
Salesforce Apex controller for Self Service Returns
public class ShopifyReturnController {
public String orderNumber {get; set;}
public String email {get; set;}
public List<OrderItem> orderItems {get; private set;}
public List<OWrapper> worderItems {get; private set;}
public Map<String, String> docLinkFiles {get; private set;}
public Integer stepNumber {get; private set;}
public String deb {get; private set;}
private Id bulkShipmentId;
public Boolean isError {get; private set;}
public String errorMessage {get; private set;}
public List<String> labels {get; private set;}
public String fromAddress {get; private set;}
public List<SelectOption> selectTypeOptions {get; private set;}
public ShopifyReturnController() {
deb = '';
labels = new List<String>();
docLinkFiles = new Map<String, String>();
orderItems = new List<OrderItem>();
worderItems = new List<OWrapper>();
selectTypeOptions = new List<SelectOption>();
selectTypeOptions.add(new SelectOption('', '--SELECT--'));
selectTypeOptions.addAll(PicklistUtils.getSelectOptions('Case', 'Reason'));
stepNumber = 1;
}
public void doForRerender() {
for (OWrapper worderItem : worderItems) {
worderItem.updateOrder();
}
}
public void doGetShippingLabel() {
Map<Id, Case> newCases = new Map<Id, Case>();
zkfedex__ShipmatePreference__c shipmatePreference = getShipmatePreference('a0V0Y000004ASAE');
bulkShipmentId = createBulkShipment(shipmatePreference);
for (OWrapper worderItem : worderItems) {
if (worderItem.isSelected == true) {
Case newCase = new Case();
newCase.AccountId = worderItem.orderItem.Order.AccountId;
newCase.ContactId = worderItem.orderItem.Order.ShipToContact.Id;
newCase.Product__c = worderItem.orderItem.Product2.Name;
newCase.Priority = 'High';
newCase.Origin = 'Phone';
newCase.Return_Email__c = email;
newCase.Order_Product__c = worderItem.orderItem.Id;
newCase.Reason = worderItem.selectedType;
newCase.Return_Reason_Extended__c = worderItem.selectedType2;
newCases.put(worderItem.orderItem.Id, newCase);
}
}
insert newCases.values();
Map<Id, zkfedex__QueuedShipment__c> queuedShipments = new Map<Id, zkfedex__QueuedShipment__c>();
for (OWrapper worderItem : worderItems) {
if (worderItem.isSelected == true) {
queuedShipments.put(worderItem.orderItem.Id, createQueuedShipment(bulkShipmentId, shipmatePreference, worderItem.orderItem, newCases.get(worderItem.orderItem.Id).Id, email));
}
}
insert queuedShipments.values();
List<zkfedex__QueuedPackage__c> queuedPackages = new List<zkfedex__QueuedPackage__c>();
Decimal packageWeight;
for (OWrapper worderItem : worderItems) {
if (worderItem.isSelected == true) {
if (worderItem.orderItem.Product2.Weight__c != null) {
packageWeight = worderItem.orderItem.Product2.Weight__c / worderItem.totalQuantity;
packageWeight = packageWeight * worderItem.selectedQantity;
}
else {
packageWeight = 0;
}
queuedPackages.add(createQueuedPackage(queuedShipments.get(worderItem.orderItem.Id).Id, packageWeight, worderItem.orderItem.TotalPrice));
}
}
insert queuedPackages;
stepNumber = 4;
}
private String progressStatus = '';
public String getJobStatus(String bulkJobId) {
List<zkfedex__BulkShipmentStatus__c> statuses = [SELECT zkfedex__Status__c FROM zkfedex__BulkShipmentStatus__c WHERE zkfedex__BatchId__c = :bulkJobId];
if(!statuses.isEmpty()) {
return statuses[0].zkfedex__Status__c;
}
return null;
}
String bulkJobId;
public void continueCreateLables() {
if (stepNumber == 4) {
if (progressStatus == '') {
bulkJobId = zkfedex.BulkShipmentInterface.validateBulkShipment(bulkShipmentId, false);
progressStatus = 'VALIDATE';
} else if (progressStatus == 'VALIDATE') {
if ( getJobStatus(bulkJobId) == 'VALIDATION_COMPLETED' ) {
Map<String, String> queuedItemStatuses = getQueuedItemStatusesMap(bulkJobId, bulkShipmentId);
for ( String status : queuedItemStatuses.values() ) {
if(status.containsIgnoreCase('ERROR')) {
isError = true;
errorMessage = status;
}
}
progressStatus = '';
stepNumber = 5;
}
}
}
else if (stepNumber == 5) {
if (progressStatus == '') {
bulkJobId = zkfedex.BulkShipmentInterface.processBulkShipment(bulkShipmentId);
progressStatus = 'PROGRESS';
}
else if (progressStatus == 'PROGRESS') {
if ( getJobStatus(bulkJobId) == 'PROCESSING_COMPLETED' ) {
Map<String, String> queuedItemStatuses = getQueuedItemStatusesMap(bulkJobId, bulkShipmentId);
for ( String status : queuedItemStatuses.values() ) {
if(status.containsIgnoreCase('ERROR')) {
isError = true;
errorMessage = status;
}
}
List<zkfedex__Shipment__c> shipments = [SELECT Id FROM zkfedex__Shipment__c WHERE Case__c IN : queuedItemStatuses.keySet()];
Map<Id, zkfedex__Shipment__c> shipmentsMap = new Map<Id, zkfedex__Shipment__c>(shipments);
List<Attachment> ats = [SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN: shipmentsMap.keySet()];
for (Attachment at : ats) {
labels.add(at.Id);
}
stepNumber = 6;
}
}
}
}
public Map<String, String> getQueuedItemStatusesMap(String bulkJobId, String bulkShipmentId) {
Map<String, String> statusesMap = new Map<String, String>();
Map<Id, zkfedex__QueuedShipment__c> queuedShipmentsMap = new Map<Id, zkfedex__QueuedShipment__c>([SELECT zkfedex__CustomAddressSourceRecordId__c FROM zkfedex__QueuedShipment__c WHERE zkfedex__BulkShipment__c = :bulkShipmentId]);
List<zkfedex__QueuedShipmentStatus__c> queuedItemsStatuses = [SELECT zkfedex__QueuedShipment__c, zkfedex__Status__c, zkfedex__StatusMessage__c FROM zkfedex__QueuedShipmentStatus__c WHERE zkfedex__BatchId__c = :bulkJobId];
for ( zkfedex__QueuedShipmentStatus__c status : queuedItemsStatuses ) {
zkfedex__QueuedShipment__c queuedShipment = queuedShipmentsMap.get(status.zkfedex__QueuedShipment__c);
String relatedContact = queuedShipment != null ? queuedShipment.zkfedex__CustomAddressSourceRecordId__c : '';
statusesMap.put(relatedContact, status.zkfedex__Status__c + ':' + status.zkfedex__StatusMessage__c);
}
return statusesMap;
}
public zkfedex__QueuedPackage__c createQueuedPackage(Id queuedShipmentId, Decimal weight, Decimal declaredValue) {
zkfedex__QueuedPackage__c queuedPackage = new zkfedex__QueuedPackage__c();
queuedPackage.zkfedex__DeclaredValue__c = declaredValue;
queuedPackage.zkfedex__Weight__c = weight;
queuedPackage.zkfedex__QueuedShipment__c = queuedShipmentId;
return queuedPackage;
}
public Id createBulkShipment(zkfedex__ShipmatePreference__c shipmatePreference) {
zkfedex__BulkShipment__c shipment = new zkfedex__BulkShipment__c();
shipment.zkfedex__ShipmatePreference__c = shipmatePreference.Id;
insert shipment;
return shipment.Id;
}
private zkfedex__ShipmatePreference__c getShipmatePreference(Id prefId) {
return [
SELECT Id, zkfedex__ShippingCity__c, zkfedex__ShippingCountry__c, zkfedex__ShippingState__c, zkfedex__ShippingStreet__c, zkfedex__ShippingPostalCode__c,
zkfedex__CompanyName__c, zkfedex__DropoffTypeDefault__c, zkfedex__EMailMessageDefault__c, zkfedex__SenderNameDefault__c, zkfedex__SenderEMailDefault__c,
zkfedex__SenderPhoneDefault__c, zkfedex__LabelImageTypeDefault__c, zkfedex__SendEMailNotificationToRecipient__c, zkfedex__SendEMailNotificationToShipper__c
FROM zkfedex__ShipmatePreference__c WHERE Id = :prefId];
}
private zkfedex__QueuedShipment__c createQueuedShipment(Id bulkShipmentId, zkfedex__ShipmatePreference__c shipmatePreference, OrderItem orderItem, Id caseId, String email) {
zkfedex__QueuedShipment__c queuedShipment = new zkfedex__QueuedShipment__c();
queuedShipment.zkfedex__BulkShipment__c = bulkShipmentId;
queuedShipment.zkfedex__ShipDate__c = Date.today();
queuedShipment.zkfedex__DropoffType__c = dropoffTypeMap.get(shipmatePreference.zkfedex__DropoffTypeDefault__c);
queuedShipment.zkfedex__LabelImageType__c = shipmatePreference.zkfedex__LabelImageTypeDefault__c;
queuedShipment.zkfedex__PackagingType__c = 'Your Packaging';
queuedShipment.zkfedex__WeightDimensionUnits__c = 'LB / IN';
queuedShipment.zkfedex__ServiceType__c = 'Domestic: FedEx First Overnight';
// queuedShipment.zkfedex__ServiceType__c = 'Domestic: FedEx Ground';
queuedShipment.zkfedex__SenderName__c = shipmatePreference.zkfedex__SenderNameDefault__c;
queuedShipment.zkfedex__SenderCompany__c = shipmatePreference.zkfedex__CompanyName__c;
queuedShipment.zkfedex__SenderPhone__c = shipmatePreference.zkfedex__SenderPhoneDefault__c;
queuedShipment.zkfedex__SenderEmail__c = shipmatePreference.zkfedex__SenderEMailDefault__c;
queuedShipment.zkfedex__SenderStreet__c = shipmatePreference.zkfedex__ShippingStreet__c;
queuedShipment.zkfedex__SenderCity__c = shipmatePreference.zkfedex__ShippingCity__c;
queuedShipment.zkfedex__SenderState__c = shipmatePreference.zkfedex__ShippingState__c;
queuedShipment.zkfedex__SenderPostalCode__c = shipmatePreference.zkfedex__ShippingPostalCode__c;
queuedShipment.zkfedex__SenderCountry__c = shipmatePreference.zkfedex__ShippingCountry__c;
queuedShipment.zkfedex__PaymentType__c = 'Sender';
queuedShipment.zkfedex__ReturnShipment__c = true;
queuedShipment.zkfedex__RecipientName__c = orderItem.Order.ShipToContact.Name;
queuedShipment.zkfedex__RecipientEmail__c = email;
queuedShipment.zkfedex__RecipientStreet__c = orderItem.Order.ShippingStreet;
queuedShipment.zkfedex__RecipientCity__c = orderItem.Order.ShippingCity;
queuedShipment.zkfedex__RecipientState__c = orderItem.Order.ShippingState;
queuedShipment.zkfedex__RecipientPostalCode__c = orderItem.Order.ShippingPostalCode;
queuedShipment.zkfedex__RecipientPhone__c = orderItem.Order.ShipToContact.Phone;
queuedShipment.zkfedex__RecipientCountry__c = orderItem.Order.ShippingCountry;
queuedShipment.zkfedex__CustomAddressSource__c = 'a050Y000000p7CrQAI';
queuedShipment.zkfedex__CustomAddressSourceRecordId__c = caseId;
return queuedShipment;
}
public void doNextSelectOrder() {
deb += worderItems;
stepNumber = 3;
}
public void findOrders() {
isError = false;
try {
orderItems = [
SELECT Id, Product2Id, Product2.Name, Product2.Weight__c, Order.AccountId, Order.ShipToContact.Id, Order.ShipToContact.Name,
Order.ShipToContact.Phone, TotalPrice, Order.ShippingStreet, Order.ShippingCountry,
Order.ShippingState, Order.ShippingCity, Order.ShippingPostalCode, Quantity
FROM OrderItem
WHERE Order.OrderNumber =: orderNumber
];
}
catch (Exception e) {
}
if (orderItems.isEmpty()) {
isError = true;
errorMessage = 'Order not found. Please check the order number again.';
} else {
fromAddress = orderItems[0].Order.ShippingStreet + ' ' + orderItems[0].Order.ShippingCity
+ ', ' + orderItems[0].Order.ShippingState + ' ' + orderItems[0].Order.ShippingPostalCode + ' ' + orderItems[0].Order.ShippingCountry;
List<String> pIds = new List<String>();
Map<Id, OWrapper> oWrapperMap = new Map<Id, OWrapper>();
for (OrderItem orderItem : orderItems) {
pIds.add(orderItem.Product2Id);
oWrapperMap.put(orderItem.Product2Id, new OWrapper(orderItem.Id, orderItem.Product2Id, orderItem));
}
List<Attachment> ats = [SELECT Id, Name, ParentId FROM Attachment WHERE ParentId IN: pIds];
for (Attachment at : ats) {
docLinkFiles.put(at.ParentId, at.Id);
if (oWrapperMap.containsKey(at.ParentId)) {
oWrapperMap.get(at.ParentId).imageId = at.Id;
}
}
worderItems.addAll(oWrapperMap.values());
stepNumber = 2;
}
}
public class OWrapper {
public String oId {get; private set;}
public String poId {get; private set;}
public String imageId {get; private set;}
public String selectedType {get; set;}
public String selectedType2 {get; set;}
public List<SelectOption> selectType2Options {get; private set;}
public Boolean isSelected {get; set;}
public OrderItem orderItem {get; private set;}
public Integer totalQuantity {get; private set;}
public Integer selectedQantity {get; set;}
public List<SelectOption> quantityOptions {get; private set;}
public OWrapper(String oId , String poId, OrderItem orderItem) {
this.oId = oId;
this.poId = poId;
this.orderItem = orderItem;
this.totalQuantity = orderItem.Quantity == null ? 0 : orderItem.Quantity.intValue();
this.selectedQantity = this.totalQuantity;
selectType2Options = new List<SelectOption>();
initQuantityOptions();
}
private void initQuantityOptions() {
quantityOptions = new List<SelectOption>();
for (Integer idx = 1; idx <= this.totalQuantity; idx++) {
quantityOptions.add(new SelectOption(''+idx, ''+idx));
}
}
public void updateOrder() {
if (isSelected == false) {
this.selectedQantity = this.totalQuantity;
}
resetSelectType2Options();
}
public void resetSelectType2Options() {
selectType2Options = PicklistUtils.getDependentSelectOptions('Case', 'Reason', 'Return_Reason_Extended__c', selectedType);
if (selectType2Options.size() == 0) {
selectedType2 = null;
}
}
}
private Map<String,String> dropoffTypeMap = new Map<String,String> {
'BUSINESS_SERVICE_CENTER' => 'Business Service Center',
'DROP_BOX' => 'Drop Box',
'REGULAR_PICKUP' => 'Regular Pickup',
'REQUEST_COURIER' => 'Request Courier',
'STATION' => 'Station'
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment