Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.84 KB | None | 0 0
  1. public with sharing class CreateOLI_Ext {
  2.  
  3. private final Opportunity opp;
  4. public string selectedPricebook {get;set;}
  5. public List<PricebookEntry> pricebookEntries {get; private set;}
  6.  
  7. public CreateOLI_Ext(ApexPages.StandardController stdController) {
  8. this.opp = (Opportunity)stdController.getRecord();
  9. List<Pricebook2> pricebooks = [SELECT Id, Name from Pricebook2 where IsActive = true];
  10. if (pricebooks.size() > 0) {
  11. selectedPricebook = pricebooks[0].Id;
  12. }
  13. //selectedPricebook = opp.Pricebook2Id;
  14. loadPricebookEntries();
  15. }
  16.  
  17. public List<SelectOption> getPricebookOptions() {
  18. List<SelectOption> options = new List<SelectOption>();
  19. List<Pricebook2> pricebooks = [SELECT Id, Name from Pricebook2 where IsActive = true];
  20. for(Pricebook2 pricebook : pricebooks) {
  21. options.add(new SelectOption(pricebook.Id,pricebook.Name));
  22. }
  23. return options;
  24. }
  25.  
  26. public void loadPricebookEntries() {
  27. pricebookEntries = [SELECT Id, Name, Product2.Name, Product2.ProductCode,
  28. UnitPrice
  29. FROM PricebookEntry
  30. WHERE Pricebook2Id =: selectedPricebook and IsActive = true];
  31. }
  32. }
  33.  
  34. <apex:page showHeader="false" sidebar="false" standardStylesheets="false" standardController="Opportunity"
  35. extensions="CreateOLI_Ext" docType="html-5.0" applyHtmlTag="false" applyBodyTag="false">
  36. <html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  37. <head>
  38. <apex:stylesheet value="{!URLFOR($Resource.SLDS0121_Order, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />
  39. </head>
  40. <body>
  41. <apex:remoteObjects >
  42. <apex:remoteObjectModel name="Product2" fields="Id,Name,ProductCode">
  43. </apex:remoteObjectModel>
  44. <apex:remoteObjectModel name="OpportunityLineItem" fields="OpportunityId,UnitPrice,PricebookEntryId,Quantity">
  45. </apex:remoteObjectModel>
  46. </apex:remoteObjects>
  47. <style>
  48. body {
  49. margin: 0;
  50. }
  51. .quantity {
  52. float: left;
  53. width: 50px;
  54. height: 30px;
  55. margin-right: 10px;
  56. }
  57. .list-group-item {
  58. margin-top: 10px;
  59. border-radius: 4px;
  60. background: #eee;
  61.  
  62. }
  63. .order-panel {
  64. border: 1px solid #ddd;
  65. border-radius: 4px;
  66. padding: 10px 15px;
  67. background: #eee;
  68. }
  69. .order-item {
  70. padding: 0;
  71. border:0;
  72. }
  73. </style>
  74. <div class="slds">
  75. <div class="slds-page-header" role="banner" style="padding: 15px;background: #E2E7ED;">
  76.  
  77. <div class="slds-grid">
  78. <div class="slds-col slds-has-flexi-truncate">
  79. <h3 class="slds-text-heading--medium slds-truncate" title="Order Form"
  80. style="font-size: 18px;">Product Selection</h3> </div>
  81. <div class="slds-col slds-no-flex slds-align-bottom">
  82. <p style="color: #8992A1;" id="header-info">
  83. No item added
  84. </p>
  85. </div>
  86. </div>
  87. </div>
  88. <apex:form style="background: #F8F9FB;">
  89. <div style="padding:20px 20px 0px 20px;">
  90. <div id="entry-list">
  91. <div class="slds-form--stacked">
  92. <div class="slds-form-element">
  93. <div class="slds-form-element__control slds-input-has-icon slds-input-has-icon--right">
  94. <svg aria-hidden="true" class="slds-input__icon slds-icon-text-default">
  95. <use xlink:href="{!URLFOR($Resource.SLDS0121_Order,'/assets/icons/utility-sprite/svg/symbols.svg#search')}"></use>
  96. </svg>
  97. <input type="text" class="slds-input search" id="search" placeholder="Enter product name or product code" />
  98.  
  99. </div>
  100. </div>
  101. <div class="slds-form-element">
  102. <apex:selectList styleClass="slds-select " id="pricebook" style="height:34px;"
  103. value="{!selectedPricebook}" multiselect="false" size="1">
  104. <apex:actionSupport event="onchange" action="{!loadPricebookEntries}" rerender="entryList"
  105. oncomplete="makeOptionList()"/>
  106. <!--<apex:selectOption itemLabel="Select a pricebook"
  107. itemValue=""></apex:selectOption>-->
  108. <apex:selectOptions value="{!PricebookOptions}" />
  109. </apex:selectList>
  110. </div>
  111. </div>
  112. <p class="slds-text-heading--label slds-truncate" style="margin-top: 20px;margin-bottom:10px;"
  113. title="Products">Products</p>
  114. <apex:outputPanel id="entryList">
  115.  
  116. <ul class="list slds-list--vertical slds-has-cards--space">
  117. <apex:repeat value="{!pricebookEntries}" var="entry">
  118. <li class="slds-list__item" style="background: white;">
  119. <div class="slds-tile slds-tile--board slds-media">
  120.  
  121. <div class="slds-media__figure">
  122. <div class="slds-icon slds-icon-standard-product">
  123. <img src="{!URLFOR($Resource.SLDS0121_Order,'/assets/icons/standard/product_60.png')}"
  124. width="40"/>
  125. </div>
  126. </div>
  127. <div class="slds-media__body">
  128. <div class="slds-grid">
  129. <div class="slds-col slds-size--3-of-4">
  130. <div class="slds-grid slds-grid--align-spread slds-has-flexi-truncate">
  131. <p class="slds-tile__title slds-truncate"><a class="name"><apex:outputField value="{!entry.Product2.Name}"/></a></p>
  132. </div>
  133. <div class="slds-tile__detail slds-text-body--small">
  134. <p class="slds-truncate code">
  135. <apex:outputField value="{!entry.Product2.ProductCode}" />
  136. </p>
  137. <p class="slds-truncate">
  138. <apex:outputField value="{!entry.UnitPrice}" />
  139. </p>
  140. </div>
  141. </div>
  142. <div class="slds-col slds-size--1-of-4">
  143. <button type="button" class="slds-button slds-button--neutral" style="float:right;width:40px;padding:0;"
  144. onclick="addToOrder('{!JSINHTMLENCODE(entry.Id)}','{!JSINHTMLENCODE(entry.Product2.Name)}','{!entry.UnitPrice}');">
  145. <img src="{!URLFOR($Resource.SLDS0121_Order,'/assets/icons/utility/add_60.png')}"
  146. width="20"/>
  147. </button>
  148. </div>
  149. </div>
  150. </div>
  151. </div>
  152. </li>
  153. </apex:repeat>
  154. </ul>
  155. </apex:outputPanel>
  156.  
  157. </div>
  158.  
  159. <p class="slds-text-heading--label slds-truncate" style="margin-top: 20px;margin-bottom:10px;"
  160. title="Products">Your Products</p>
  161. <p class="slds-text-body--small" id="order-placeholder">
  162. No product added yet...
  163. </p>
  164. </div>
  165. <div id="order-table" style="display:none;">
  166. <table class="slds-table slds-table--bordered">
  167. <thead>
  168. <tr class="slds-text-heading--label">
  169. <th scope="col">
  170. <span class="slds-truncate">QTY</span>
  171. </th>
  172. <th scope="col">
  173. <span class="slds-truncate">Product</span>
  174. </th>
  175. <th scope="col">
  176. <span class="slds-truncate"></span>
  177. </th>
  178. </tr>
  179. </thead>
  180. <tbody id="order-list">
  181. <!-- sample entry
  182. <tr class="slds-hint-parent">
  183. <td data-label="QTY">
  184. <span class="slds-truncate">
  185. <input type="number" class="slds-input" style="width:50px;"/>
  186. </span>
  187. </td>
  188. <td data-label="Product name">
  189. <a class="slds-truncate">Product name</a>
  190. </td>
  191. <td class="slds-row-action">
  192. <div style="float:right;">
  193. <svg aria-hidden="true" class="slds-button__icon slds-button__icon--hint slds-button__icon--medium">
  194. <use xlink:href="{!URLFOR($Resource.SLDS0121_Order,'/assets/icons/utility-sprite/svg/symbols.svg#close')}"></use>
  195. </svg>
  196. <span class="slds-assistive-text">Remove</span>
  197. </div>
  198. </td>
  199. </tr>-->
  200. </tbody>
  201. </table>
  202.  
  203. </div>
  204. <div style="padding: 20px; min-height: 80px;">
  205. <div style="float:right;">
  206. <button type="button" onclick="RefreshButton()" class="slds-button slds-button--brand " id="refresh" disabled="false">
  207. Refresh
  208. </button>
  209. <button type="button" onclick="clearOrder()" class="slds-button slds-button--neutral ">
  210. Clear
  211. </button>
  212. <button type="button" onclick="createOrder()" class="slds-button slds-button--brand" id="submit" disabled="disabled">
  213. Add
  214. </button>
  215.  
  216. </div>
  217. </div>
  218.  
  219. </apex:form>
  220. </div>
  221. <apex:includeScript value="{!URLFOR($Resource.FastOrderJs, 'jquery.min.js')}"/>
  222. <apex:includeScript value="{!URLFOR($Resource.FastOrderJs, 'list.min.js')}"/>
  223. <script src="/support/console/35.0/integration.js" type="text/javascript"></script>
  224. <script>
  225. var DisableFlag = 'true';
  226. //sforce.console.openPrimaryTab(null, '/' + orderId, true);
  227. var orders = [];
  228. var options = {
  229. valueNames: [ 'name', 'code'],
  230. page: 5,
  231. };
  232. var entryList = new List('entry-list', options);
  233. function makeOptionList() {
  234. entryList = new List('entry-list', options);
  235. RefreshButtonDisplay(DisableFlag);
  236.  
  237. }
  238. function updateHeaderInfo() {
  239. if (orders.length === 0) {
  240. $('#header-info').html('No item added');
  241. } else if (orders.length === 1) {
  242. $('#header-info').html('<a>1 item added</a>');
  243. } else {
  244. $('#header-info').html('<a>' + orders.length + ' items added</a>');
  245. }
  246. }
  247. function addToOrder(entryId, entryName, entryPrice) {
  248. $('#order-table').css('display','block');
  249. $('#order-placeholder').css('display','none');
  250. $('#submit').prop('disabled',false);
  251. var index = findOrderItem(entryId);
  252. if (index > -1) {
  253. ++orders[index].Quantity;
  254. $('#' + entryId).val(orders[index].Quantity);
  255. } else {
  256. orders.push({ Id: entryId, Price: entryPrice, Quantity: 1});
  257. $('#order-list').append('<tr class="slds-hint-parent"><td data-label="QTY">' +
  258. '<span class="slds-truncate">' +
  259. '<input type="number" class="slds-input quantity" style="width:60px;border:none;"' +
  260. ' value="1" min="1" onchange="changeQuantity(this, '' +
  261. entryId + '')" id="'+ entryId + '"/> ' +
  262. '</span></td><td data-label="Product Name"><a class="slds-truncate">' +
  263. entryName + '</a></td><td class="slds-row-action"><div style="float:right;cursor:pointer;" onclick="removeEntry(this, ''
  264. + entryId + '')">' +
  265. '<svg aria-hidden="true" class="slds-button__icon slds-button__icon--hint slds-button__icon--medium">' +
  266. '<use xlink:href="' +
  267. "{!URLFOR($Resource.SLDS0121_Order,'/assets/icons/utility-sprite/svg/symbols.svg#close')}" +
  268. '"></use>' +
  269. '</svg></div></td></tr>'
  270. );
  271. }
  272. updateHeaderInfo();
  273. }
  274. function changeQuantity(quantity, entryId) {
  275. var index = findOrderItem(entryId);
  276. orders[index].Quantity = $(quantity).val();
  277. }
  278. function removeEntry(quantity, entryId) {
  279. var index = findOrderItem(entryId);
  280. if (index > -1) {
  281. orders.splice(index, 1);
  282. $(quantity).parent().parent().remove();
  283. }
  284. if (orders.length === 0) {
  285. $('#order-placeholder').css('display','block');
  286. $('#order-table').css('display','none');
  287. $('#submit').prop('disabled',true);
  288. }
  289. updateHeaderInfo();
  290. }
  291. function findOrderItem(entryId) {
  292. for(var i = 0; i < orders.length; ++i) {
  293. if (orders[i].Id === entryId) {
  294. return i;
  295. }
  296. }
  297. return -1;
  298. }
  299. function clearOrder() {
  300. orders = [];
  301. $('#order-list').html('');
  302. $('#order-placeholder').css('display','block');
  303. $('#order-table').css('display','none');
  304. $('#submit').prop('disabled',true);
  305. $('#search').val('');
  306. updateHeaderInfo();
  307. }
  308. function createOrder() {
  309. //var details = {
  310. // AccountId: '{!Opportunity.Account.Id}',
  311. // Status: 'Draft',
  312. // EffectiveDate: new Date(),
  313. // Pricebook2Id: $('select[id*="pricebook"]').val(),
  314. //};
  315. //var order = new SObjectModel.Order();
  316. //order.create(details, function(err) {
  317. // if(err) {
  318. // console.log(err);
  319. // alert(err.message);
  320. // }
  321. // else {
  322. // console.log('order created: ' + order.get('Id')); // Id is set when create completes
  323. // createOrderItems(order.get('{!Opportunity.Id}'));
  324. // }
  325. //});
  326.  
  327. createOrderItems('{!Opportunity.Id}');
  328. clearOrder();
  329. //RefreshPrimaryTab();
  330. DisableFlag = "false";
  331. RefreshButtonDisplay(DisableFlag);
  332. }
  333.  
  334. function createOrderItems(orderId) {
  335. for (var i = 0; i < orders.length; ++i) {
  336. var details = {
  337. OpportunityId: orderId,
  338. UnitPrice: orders[i].Price,
  339. PricebookEntryId: orders[i].Id,
  340. Quantity: orders[i].Quantity,
  341. };
  342. var orderItem = new SObjectModel.OpportunityLineItem();
  343. orderItem.create(details, function(err) {
  344. if(err) {
  345. console.log(err);
  346. alert(err.message);
  347. }
  348. else {
  349. // this is the OLI
  350. console.log(orderItem.log()); // Dump OLI to log
  351. console.log(orderItem.get('Id')); // Id is set when create completes
  352. }
  353. });
  354. }
  355. openSubtab('/' + orderId);
  356. }
  357.  
  358. function openSubtab(subtabUrl)
  359. {
  360. // Use the results primary tab id and open a new subtab
  361. //var openSubtab = function(result)
  362. //{
  363. // sforce.console.openSubtab(result.id, subtabUrl, true);
  364. //};
  365. //sforce.console.getEnclosingPrimaryTabId(openSubtab);
  366.  
  367. }
  368.  
  369. function RefreshButtonDisplay(dFlag)
  370. {
  371. if(dFlag == 'true') {
  372. $('#refresh').prop('disabled',true);
  373. }
  374. else {
  375. $('#refresh').prop('disabled',false);
  376. }
  377. }
  378.  
  379. function RefreshButton()
  380. {
  381. RefreshPrimaryTab();
  382. DisableFlag = "true";
  383. RefreshButtonDisplay(DisableFlag);
  384. }
  385.  
  386. function RefreshPrimaryTab()
  387. {
  388. sforce.console.getFocusedPrimaryTabId(showTabId);
  389. }
  390.  
  391. var showTabId = function showTabId(result)
  392. {
  393. var tabId = result.id;
  394. //alert('Primary Tab IDs: primaryTabId ' + tabId );
  395. sforce.console.refreshPrimaryTabById(tabId , true, refreshSuccess);
  396. };
  397.  
  398. var refreshSuccess = function refreshSuccess(result)
  399. {
  400. //Report whether refreshing the primary tab was successful
  401. if (result.success == true)
  402. {
  403. //alert('Primary tab refreshed successfully');
  404. RefreshButtonDisplay(DisableFlag);
  405. }
  406. else
  407. {
  408. //alert('Primary did not refresh');
  409. }
  410. };
  411.  
  412. </script>
  413.  
  414. </body>
  415. </html>
  416. </apex:page>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement