Advertisement
Guest User

Untitled

a guest
May 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. private Id bulkShipmentId;
  2. private String batchId;
  3. public final String workingObject = 'ChargentOrders__ChargentOrder__c';
  4. global String scope;
  5.  
  6. global Database.QueryLocator start(Database.BatchableContext BC) {
  7.  
  8. String orderStatus = 'Initiated';
  9. return Database.getQueryLocator([SELECT Id FROM ChargentOrders__ChargentOrder__c WHERE ChargentOrders__Status__c = :orderStatus]);
  10. }
  11.  
  12. global void execute(Database.BatchableContext BC, List<ChargentOrders__ChargentOrder__c> scope) {
  13.  
  14. if(scope.size() > 0){
  15.  
  16. List<zkusps__ShippingPreference__c> caseShipmateList = [SELECT zkusps__CompanyName__c, zkusps__ShippingCity__c, zkusps__preference__c, zkusps__shippingSenderEmailDefault__c,
  17. zkusps__shippingSenderPhoneDefault__c, zkusps__ShippingState__c, zkusps__ShippingStreet__c,
  18. zkusps__ShippingPostalCode__c, zkusps__LabelImageTypeDefault__c FROM zkusps__ShippingPreference__c LIMIT 1];
  19.  
  20.  
  21. if ( ! caseShipmateList.isEmpty() ) {
  22.  
  23. // 1. zkusps__BulkShipment__c object
  24. zkusps__BulkShipment__c bulkShipment = new zkusps__BulkShipment__c (
  25. zkusps__BillingPreference__c = caseShipmateList.get(0).zkusps__preference__c
  26. );
  27. insert bulkShipment;
  28. bulkShipmentId = bulkShipment.Id;
  29.  
  30. List<zkusps__CustomAddressSource__c> customAddressSourceList = [SELECT Id FROM zkusps__CustomAddressSource__c WHERE zkusps__CustomAddressSourceObject__c =
  31. :workingObject LIMIT 1];
  32.  
  33.  
  34. Set<Id> orderIds = new Set<Id>();
  35. for(ChargentOrders__ChargentOrder__c order : scope) {
  36. orderIds.add(order.Id);
  37. }
  38.  
  39. /* Get all unshiped Orders */
  40. List<ChargentOrders__ChargentOrder__c> orders = [ SELECT ChargentOrders__Shipping_City__c, ChargentOrders__Shipping_Country__c, ChargentOrders__Billing_Email__c,
  41. ChargentOrders__Billing_First_Name__c, ChargentOrders__Billing_Last_Name__c, ChargentOrders__Billing_Phone__c,
  42. ChargentOrders__Shipping_State__c, ChargentOrders__Shipping_Address__c, ChargentOrders__Shipping_Zip_Postal__c
  43. FROM ChargentOrders__ChargentOrder__c WHERE Id IN : orderIds];
  44.  
  45.  
  46.  
  47. if ( ! customAddressSourceList.isEmpty() ) {
  48.  
  49. // 2. zkusps__QueuedShipment__c object
  50. zkusps__QueuedShipment__c[] queueItems = new zkusps__QueuedShipment__c[]{};
  51.  
  52. for(ChargentOrders__ChargentOrder__c order : orders){
  53.  
  54. zkusps__QueuedShipment__c queuedShipment = new zkusps__QueuedShipment__c (
  55. zkusps__BulkShipment__c = bulkShipment.Id,
  56. zkusps__LabelImageType__c = caseShipmateList.get(0).zkusps__LabelImageTypeDefault__c,
  57. zkusps__ServiceType__c = 'Domestic: Express',
  58. zkusps__PackagingType__c = 'Parcel',
  59.  
  60. // sender info
  61. zkusps__SenderName__c = caseShipmateList.get(0).zkusps__CompanyName__c,
  62. zkusps__SenderCity__c = caseShipmateList.get(0).zkusps__ShippingCity__c,
  63. zkusps__SenderCompany__c = caseShipmateList.get(0).zkusps__CompanyName__c,
  64. zkusps__SenderEmail__c = caseShipmateList.get(0).zkusps__shippingSenderEmailDefault__c,
  65. zkusps__SenderPhone__c = caseShipmateList.get(0).zkusps__shippingSenderPhoneDefault__c,
  66. zkusps__SenderState__c = caseShipmateList.get(0).zkusps__ShippingState__c,
  67. zkusps__SenderStreet__c = caseShipmateList.get(0).zkusps__ShippingStreet__c,
  68. zkusps__SenderPostalCode__c = caseShipmateList.get(0).zkusps__ShippingPostalCode__c,
  69.  
  70. // recipient info
  71. zkusps__RecipientCity__c = order.ChargentOrders__Shipping_City__c,
  72. zkusps__RecipientCompany__c = '',
  73. zkusps__RecipientCountry__c = 'US',
  74. zkusps__RecipientEmail__c = order.ChargentOrders__Billing_Email__c,
  75. zkusps__RecipientName__c = order.ChargentOrders__Billing_First_Name__c,
  76. zkusps__RecipientPhone__c = order.ChargentOrders__Billing_Phone__c,
  77. zkusps__RecipientState__c = order.ChargentOrders__Shipping_State__c,
  78. zkusps__RecipientStreet__c = order.ChargentOrders__Shipping_Address__c,
  79. zkusps__RecipientPostalCode__c = order.ChargentOrders__Shipping_Zip_Postal__c,
  80. zkusps__CustomAddressSource__c = customAddressSourceList.get(0).id,
  81. zkusps__CustomAddressSourceRecordId__c = order.Id);
  82.  
  83. queueItems.add(queuedShipment);
  84. }
  85. insert queueItems;
  86.  
  87. List<zkusps__QueuedShipment__c> queuedShipmentIds = [SELECT Id FROM zkusps__QueuedShipment__c WHERE zkusps__CustomAddressSourceRecordId__c IN : orderIds];
  88.  
  89. // 3. zkusps__QueuedPackage__c object
  90. zkusps__QueuedPackage__c[] queuedPackages = new zkusps__QueuedPackage__c[]{};
  91. for(zkusps__QueuedShipment__c queueId : queuedShipmentIds){
  92.  
  93. zkusps__QueuedPackage__c queuedPackage = new zkusps__QueuedPackage__c (
  94. zkusps__DeclaredValue__c = 0,
  95. zkusps__WeightPounds__c = 0,
  96. zkusps__QueuedShipment__c = queueId.Id
  97. );
  98. queuedPackages.add(queuedPackage);
  99. }
  100. insert queuedPackages;
  101.  
  102. // Validate shipping address
  103. batchId = zkusps.BulkShipmentInterface.validateBulkShipment(bulkShipmentId);
  104.  
  105. zkusps__BulkShipmentStatus__c statuses = new zkusps__BulkShipmentStatus__c();
  106.  
  107. statuses.zkusps__BulkShipment__c = bulkShipmentId;
  108. statuses.zkusps__Status__c = 'VALIDATION_COMPLETED';
  109. insert statuses;
  110.  
  111. batchId = zkusps.BulkShipmentInterface.processBulkShipment(bulkShipmentId);
  112.  
  113. }else {
  114.  
  115. System.debug('Invalid Custom Address Source.');
  116. return;
  117. }
  118. }else {
  119.  
  120. System.debug('Invalid Preference.');
  121. return;
  122. }
  123.  
  124. } else{
  125.  
  126. System.debug('0 order found with status "Initiated"');
  127. return;
  128. }
  129. }
  130.  
  131. global void finish(Database.BatchableContext BC) {
  132.  
  133. }
  134.  
  135. @testsetup
  136. /* For data test */
  137. static void testData() {
  138.  
  139. /* Order info */
  140. ChargentOrders__ChargentOrder__c chargeOrder = new ChargentOrders__ChargentOrder__c();
  141. chargeOrder.ChargentOrders__Status__c = 'Initiated';
  142. chargeOrder.ChargentOrders__Shipping_City__c = 'Chicago';
  143. chargeOrder.ChargentOrders__Shipping_Country__c = 'United States';
  144. chargeOrder.ChargentOrders__Billing_Email__c = 'francine@apothecarybeauty.com';
  145. chargeOrder.ChargentOrders__Billing_First_Name__c = 'Erin';
  146. chargeOrder.ChargentOrders__Billing_Last_Name__c = 'Lang';
  147. chargeOrder.ChargentOrders__Billing_Phone__c = '(312) 813-9876';
  148. chargeOrder.ChargentOrders__Shipping_State__c = 'IL';
  149. chargeOrder.ChargentOrders__Shipping_Address__c = '1050 Marshfield Ave #3';
  150. chargeOrder.ChargentOrders__Shipping_Zip_Postal__c = '60622';
  151. insert chargeOrder;
  152.  
  153. /* zenkraft preference info */
  154. zkusps__Preference__c testbillingzkusps = new zkusps__Preference__c();
  155. testbillingzkusps.zkusps__accountContactEmail__c ='salesforce@mcnabbus.com';
  156. testbillingzkusps.zkusps__accountContactName__c ='Mcnabb SalesforceAdmin';
  157. insert testbillingzkusps;
  158.  
  159. /* zenkraft shipping preference info*/
  160. zkusps__ShippingPreference__c testzkusps = new zkusps__ShippingPreference__c();
  161. testzkusps.zkusps__CompanyName__c = 'McNabb Technologies LLC';
  162. testzkusps.zkusps__ShippingCity__c = 'Chicago';
  163. testzkusps.zkusps__preference__c = testbillingzkusps.Id;
  164. testzkusps.zkusps__shippingSenderEmailDefault__c = 'salesforce@mcnabbus.com';
  165. testzkusps.zkusps__shippingSenderPhoneDefault__c = '3109785333';
  166. testzkusps.zkusps__ShippingState__c = 'IL';
  167. testzkusps.zkusps__ShippingStreet__c = '1621 W Walnut St';
  168. testzkusps.zkusps__ShippingPostalCode__c = '60612';
  169. testzkusps.zkusps__LabelImageTypeDefault__c = 'ZPLII';
  170. insert testzkusps;
  171.  
  172. /* zenkraft bulkshipment info */
  173. zkusps__BulkShipment__c testbulkShipment = new zkusps__BulkShipment__c ();
  174. testbulkShipment.zkusps__BillingPreference__c = testbillingzkusps.Id;
  175. insert testbulkShipment;
  176.  
  177. /* zenkraft custom address source info */
  178. zkusps__CustomAddressSource__c testcustomadd = new zkusps__CustomAddressSource__c();
  179. testcustomadd.zkusps__CustomAddressSourceObject__c = 'ChargentOrders__ChargentOrder__c';
  180. insert testcustomadd;
  181.  
  182. /* zenkraft queued shipment info */
  183. zkusps__QueuedShipment__c testqueuedship = new zkusps__QueuedShipment__c();
  184. testqueuedship.zkusps__BulkShipment__c =testbulkShipment.Id;
  185. testqueuedship.zkusps__LabelImageType__c =testzkusps.zkusps__labelImageTypeDefault__c;
  186. testqueuedship.zkusps__ServiceType__c ='Domestic: Express';
  187. testqueuedship.zkusps__PackagingType__c ='Parcel';
  188. // sender info
  189. testqueuedship.zkusps__SenderName__c = testzkusps.zkusps__CompanyName__c;
  190. testqueuedship.zkusps__SenderCity__c = testzkusps.zkusps__ShippingCity__c;
  191. testqueuedship.zkusps__SenderCompany__c = testzkusps.zkusps__CompanyName__c;
  192. testqueuedship.zkusps__SenderEmail__c = testzkusps.zkusps__shippingSenderEmailDefault__c;
  193. testqueuedship.zkusps__SenderPhone__c = testzkusps.zkusps__shippingSenderPhoneDefault__c;
  194. testqueuedship.zkusps__SenderState__c = testzkusps.zkusps__ShippingState__c;
  195. testqueuedship.zkusps__SenderStreet__c = testzkusps.zkusps__ShippingStreet__c;
  196. testqueuedship.zkusps__SenderPostalCode__c = testzkusps.zkusps__ShippingPostalCode__c;
  197. // recipient info
  198. testqueuedship.zkusps__RecipientCity__c = chargeOrder.ChargentOrders__Shipping_City__c;
  199. testqueuedship.zkusps__RecipientCompany__c = '';
  200. testqueuedship.zkusps__RecipientCountry__c = 'US';
  201. testqueuedship.zkusps__RecipientEmail__c = chargeOrder.ChargentOrders__Billing_Email__c;
  202. testqueuedship.zkusps__RecipientName__c = chargeOrder.ChargentOrders__Billing_First_Name__c;
  203. testqueuedship.zkusps__RecipientPhone__c = chargeOrder.ChargentOrders__Billing_Phone__c;
  204. testqueuedship.zkusps__RecipientState__c = chargeOrder.ChargentOrders__Shipping_State__c;
  205. testqueuedship.zkusps__RecipientStreet__c = chargeOrder.ChargentOrders__Shipping_Address__c;
  206. testqueuedship.zkusps__RecipientPostalCode__c = chargeOrder.ChargentOrders__Shipping_Zip_Postal__c;
  207. testqueuedship.zkusps__CustomAddressSource__c = testcustomadd.id;
  208. testqueuedship.zkusps__CustomAddressSourceRecordId__c = chargeOrder.Id;
  209. insert testqueuedship;
  210.  
  211. /* zenkraft queued package info */
  212. zkusps__QueuedPackage__c testqueuedpack = new zkusps__QueuedPackage__c();
  213. testqueuedpack.zkusps__DeclaredValue__c = 0;
  214. testqueuedpack.zkusps__WeightPounds__c = 0;
  215. testqueuedpack.zkusps__QueuedShipment__c = testqueuedship.Id;
  216. insert testqueuedpack;
  217.  
  218. /* zenkraft bulk shipment status info */
  219. zkusps__BulkShipmentStatus__c testbulkship = new zkusps__BulkShipmentStatus__c();
  220. testbulkship.zkusps__BulkShipment__c = testbulkShipment.Id;
  221. testbulkship.zkusps__Status__c = 'VALIDATION_COMPLETED';
  222. insert testbulkship;
  223.  
  224. }
  225.  
  226. /* For Scheduler test */
  227. static testMethod void testScheduler() {
  228. System.schedule('Test Checker', '0 0 0/1 1/1 * ? *', new ZenKraftBatchScheduler());
  229. }
  230.  
  231. /* For Batch test */
  232. static testMethod void testBatch() {
  233. MakeShipmentQueue objBatch = new MakeShipmentQueue();
  234. Database.executeBatch(objBatch);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement