Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.27 KB | None | 0 0
  1. <apex:page controller="ForecastByWarehouseController" title="Forecast By Warehouse">
  2.  
  3. <script>
  4. var elementFocus = null;
  5. function retainFocus()
  6. {
  7. document.getElementById(elementFocus.name).focus();
  8. }
  9.  
  10. function JS_QtyChanged(ele)
  11. {
  12. console.log(ele);
  13. console.log(ele.parentNode.parentNode.childNodes);
  14. var sum = 0;
  15. var i;
  16. var nodes=ele.parentNode.parentNode.childNodes;
  17. for(i in ele.parentNode.parentNode.childNodes)
  18. {
  19. console.log(nodes[i].className);
  20. if(nodes[i].className == "quantityTableCell"){
  21. var className = nodes[i].childNodes[0].className;
  22. if((className=="quantityCellRed" || className=="quantityCell"))
  23. {
  24. var val = nodes[i].childNodes[0].value;
  25. console.log(val);
  26. if(!isNaN(val) && val !='undefined' && val != null && val != '')
  27. {
  28. //alert(val);
  29. sum += parseFloat(val);
  30. }
  31.  
  32. }
  33. }
  34.  
  35.  
  36. if(nodes[i].className == "quantityTotalCell"){
  37. oldvalue = parseInt(nodes[i].innerHTML);
  38. if(oldvalue != sum){
  39. ele.className = "quantityCellRed";
  40. }
  41. var n = sum.toFixed(2);
  42. nodes[i].innerHTML = n;
  43. }
  44. }
  45. //call_qtyChanged();
  46. }
  47. </script>
  48. <apex:form id="frm">
  49. <apex:pageblock >
  50. <apex:pageBlockSection columns="3" title="Select Warehouse">
  51. <apex:inputField value="{!objProdInventory.gii__Warehouse__c}" label="Warehouse"/>
  52. <apex:inputField value="{!objForecast.Sales_Year__c}" label="Sales Year"/>
  53. <apex:commandButton value="Submit" action="{!fetchProducts}" rerender="frm"/>
  54. </apex:pageBlockSection>
  55. </apex:pageblock>
  56. <apex:pageblock id="pb_filter" rendered="{!warehouseSelected }">
  57. <apex:pageBlockSection columns="3" title="Filter Products">
  58. <apex:selectList size="1" label="Search By" value="{!strFilterBy}">
  59. <apex:selectOptions value="{!filterBy}"/>
  60. </apex:selectList>
  61. <apex:inputText value="{!searchText}" label="Search Text"/>
  62. <apex:commandButton value="Search" action="{!searchProducts}" rerender="pb_products"/>
  63. </apex:pageBlockSection>
  64. </apex:pageblock>
  65. <apex:outputpanel id="pb_products" >
  66. <apex:pageBlock rendered="{!IF(lstProdWrapper.size>0,true,false)}">
  67. <apex:pageBlockButtons >
  68. <apex:commandButton value="Delete" action="{!del}"/>
  69. <apex:commandButton value="Save" action="{!save}"/>
  70. <apex:commandButton value="Save & Exit" action="{!saveNexit}"/>
  71.  
  72. </apex:pageBlockButtons>
  73. <center>
  74. <apex:outputPanel layout="block" styleClass="pSearchShowMore" id="otpNav">
  75.  
  76. <apex:image url="/img/search_prevarrow_disabled.gif" styleClass="prevArrow" rendered="{!NOT(Con.HasPrevious)}"/>
  77. <apex:image url="/img/search_prevarrow.gif" title="Previous Page" styleClass="prevArrow" rendered="{!Con.HasPrevious}"/>
  78. <apex:commandLink action="{!Previous}" title="Previous Page" value="Previous Page" rendered="{!HasPrevious}" reRender="pb_products"/>
  79. <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(HasPrevious)}">Previous Page</apex:outputPanel>
  80. &nbsp;({!IF(PageNumber == 0,1,(PageNumber * PageSize)+1)}-{!IF(CountTotalRecords != null && CountTotalRecords < PageSize,CountTotalRecords,((PageNumber+1) * PageSize))})&nbsp;
  81. <apex:outputPanel styleClass="pShowLess noLink" style="color:grey" rendered="{!NOT(HasNext)}" >Next Page</apex:outputPanel>
  82. <apex:commandLink title="Next Page" value="Next Page" rendered="{!HasNext}" action="{!Next}" reRender="pb_products"/>&nbsp;
  83. <apex:image url="/img/search_nextarrow.gif" title="Next Page" styleClass="nextArrow" rendered="{!Con.HasNext}"/>
  84. <apex:image url="/img/search_nextarrow_disabled.gif" rendered="{!NOT(Con.HasNext)}"/>
  85. </apex:outputPanel>
  86. </center><br/>
  87.  
  88. <apex:outputpanel id="tablevalues" >
  89. <table style="width:100%">
  90. <tr>
  91. <th>{!$Label.Action}</th>
  92. <th>{!$Label.Forecast_Product_Code}</th>
  93. <th>{!$Label.Forecast_Product_SKU}</th>
  94. <apex:repeat value="{!allMonths}" var="month">
  95. <th align="center">{!month}</th>
  96. </apex:repeat>
  97. <th>{!$Label.Forecast_Total}</th>
  98. </tr>
  99. <apex:repeat value="{!lstProdWrapper}" var="prod">
  100. <tr>
  101. <td><apex:inputCheckbox value="{!prod.checked}"/></td>
  102. <td width="140px">{!prod.productSKU}</td>
  103. <td width="80px">{!prod.productCode}</td>
  104. <apex:variable value="{!0}" var="count"/>
  105. <apex:repeat value="{!allMonths}" var="mon">
  106. <td class="quantityTableCell">
  107. <apex:inputText value="{!prod.monthQty[mon]}" styleClass="{!if(prod.cellEdited[mon],'quantityCellRed','quantityCell')}" onchange="JS_QtyChanged(this)"/>
  108. </td>
  109. </apex:repeat>
  110. <td align="right" padding-top= "2px" class="quantityTotalCell">
  111. <b><apex:outputText value="{!prod.total}"/></b>
  112. </td>
  113. </tr>
  114. </apex:repeat>
  115. </table>
  116. </apex:outputpanel>
  117. </apex:pageBlock>
  118. <apex:outputpanel rendered="{!IF(lstProdWrapper.size==0 && warehouseSelected ,true,false)}">
  119. <apex:pageBlock >
  120. {!$Label.No_Result_Found}
  121. </apex:pageBlock>
  122. </apex:outputpanel>
  123. </apex:outputpanel>
  124. </apex:form>
  125. </apex:page>
  126.  
  127. global class ForecastByWarehouseController
  128. {
  129. public ApexPages.StandardSetController con{get; set;}
  130. public List<gii__ProductInventory__c> lstProdInventory{get;set;}
  131. public gii__ProductInventory__c objProdInventory{get;set;}
  132. public gii__Warehouse__c objWarehouse{get;set;}
  133. public Forecast__c objForecast{get;set;}
  134. public String Searchtext{get;set;}
  135. public List<ProductWrapper> lstProdWrapper{get;set;}
  136. public String strFilterBy{get;set;}
  137. public boolean noResultFound {get;set;}
  138. public boolean warehouseSelected {get;set;}
  139. public Integer CountTotalRecords{get;set;}
  140. public Integer NoOfPages{get;set;}
  141. public Integer PageSize{get;set;}
  142. public Integer PageNumber{get;set;}
  143. public String soql;
  144. public List<Forecast__c> allForecast;
  145. public gii__Warehouse__c selectedWarehouse;
  146. public Sales_Year__c selectSalesYear;
  147. public List<String> allMonths{get;set;}
  148. public String filterQueryPart;
  149.  
  150. public ForecastByWarehouseController()
  151. {
  152. Searchtext = '';
  153. strFilterBy = '';
  154. objProdInventory = new gii__ProductInventory__c();
  155. lstProdInventory = new List<gii__ProductInventory__c>();
  156. objForecast = new Forecast__c();
  157. lstProdWrapper = new List<ProductWrapper>();
  158. PageSize = 10;
  159. PageNumber = 0;
  160. warehouseSelected = false;
  161. allMonths = new List<String>{'January','February','March','April','May','June','July','August','September','October', 'November','December'};
  162. }
  163.  
  164. public List<SelectOption> getFilterBy()
  165. {
  166. List<SelectOption> options = new List<SelectOption>();
  167. options.add(new SelectOption('All','All Products'));
  168. options.add(new SelectOption('gii__Family__c','Product Family'));
  169. options.add(new SelectOption('gii__Color__r.name','Color'));
  170. options.add(new SelectOption('gii__ProductStyle__r.name','Style'));
  171. options.add(new SelectOption('gii__Size__c','Size'));
  172. return options;
  173. }
  174.  
  175. public void addFilters()
  176. {
  177. system.debug('n--strFilterBy--'+strFilterBy+'n--Constants.STYLEFILTER--'+Constants.STYLEFILTER);
  178. if((strFilterBy != '' && searchText != '')&& (strFilterBy != 'All') && ((strFilterBy == Constants.STYLEFILTER) || (strFilterBy == Constants.COLORFILTER) || (strFilterBy == Constants.FAMILYFILTER) ))
  179. {
  180. filterQueryPart = ' AND gii__Product__r.' + strFilterBy + ' LIKE '%' + searchText + '%'';
  181. }
  182. else if((strFilterBy != '' && searchText != '') && (strFilterBy == 'All') )
  183. {
  184. filterQueryPart = ' AND gii__Product__r.Name LIKE '%' + searchText + '%'';
  185. }
  186. else
  187. filterQueryPart = '';
  188. system.debug('n--filterQueryPart--'+filterQueryPart);
  189. }
  190.  
  191. public PageReference searchProducts()
  192. {
  193. fetchProducts();
  194. return null;
  195. }
  196.  
  197. public PageReference fetchProducts()
  198. {
  199. PageSize = 10;
  200. PageNumber = 0;
  201. warehouseSelected = true;
  202. selectedWarehouse = [select id,name from gii__Warehouse__c where id=:objProdInventory.gii__Warehouse__c];
  203. selectSalesYear = [select id,name from Sales_Year__c where id=:objForecast.Sales_Year__c];
  204. createWrapper();
  205. return null;
  206. }
  207.  
  208. public void createWrapper()
  209. {
  210. lstProdWrapper = new List<ProductWrapper>();
  211. addFilters();
  212. system.debug('n--searchText--'+searchText);
  213. soql = 'Select id,gii__Product__r.gii__ProductStyle__r.name,name,gii__Product__c,gii__Product__r.name,gii__Product__r.gii__ProductCode__c,gii__Warehouse__c from gii__ProductInventory__c where gii__Product__r.gii__Style__c <> true AND gii__Warehouse__c ='' + selectedWarehouse.id + '' ' + filterQueryPart + ' Order By gii__Product__r.gii__ProductStyle__r.name NULLS LAST,gii__Product__r.name limit ' + PageSize + ' offset ' + PageNumber*PageSize;
  214. String soql4count = 'Select count() from gii__ProductInventory__c where gii__Warehouse__c ='' + selectedWarehouse.id + ''';
  215. CountTotalRecords= Database.countQuery(soql4count);
  216. NoOfPages = CountTotalRecords/PageSize;
  217. if(NoOfPages * PageSize < CountTotalRecords)
  218. NoOfPages++;
  219. System.debug(soql);
  220. lstProdInventory = Database.query(soql);
  221. allForecast = [select id,Product_Inventory__c,Sales_Year__c, (select id,name,Quantity__c,Month__c from Forecast_Details__r) from Forecast__c where Product_Inventory__c IN: lstProdInventory and Sales_Year__c =: selectSalesYear.id ];
  222. Decimal totalForecast = 0;
  223. for(gii__ProductInventory__c pi : lstProdInventory)
  224. {
  225. ProductWrapper pw = new ProductWrapper();
  226. pw.productstyle = pi.gii__Product__r.gii__ProductStyle__r.name;
  227. pw.monthQty = new Map<String,String>();
  228. pw.oldMonthQty = new Map<String,String>();
  229. pw.idMonthQty = new Map<String,String>();
  230. pw.cellEdited = new Map<String,Boolean>();
  231. pw.productSKU = pi.gii__Product__r.name;
  232. pw.productCode = pi.gii__Product__r.gii__ProductCode__c;
  233. pw.ProForecast = createForecast(pi,selectSalesYear);
  234. totalForecast = 0;
  235. for(Forecast_Detail__c fd : pw.ProForecast.Forecast_Details__r)
  236. {
  237. pw.monthQty.put(fd.Month__c,String.valueOf(fd.Quantity__c));
  238. pw.oldMonthQty.put(fd.Month__c,String.valueOf(fd.Quantity__c));
  239. pw.IdMonthQty.put(fd.Month__c,fd.id);
  240. system.debug('n--fd.Quantity__c;--'+fd.Quantity__c);
  241. totalForecast += fd.Quantity__c;
  242. }
  243. pw.total = String.valueOf(totalForecast);
  244. system.debug('n--pw.total--'+pw.total);
  245. for(integer i=0;i<12;i++)
  246. {
  247. if(!pw.monthQty.keyset().contains(allMonths[i]))
  248. {
  249. pw.monthQty.put(allMonths[i],'');
  250. pw.oldMonthQty.put(allMonths[i],'xxx');
  251. }
  252. pw.cellEdited.put(allMonths[i],false);
  253. }
  254. system.debug('n--pw.total--'+pw.total);
  255. system.debug('n--SIZEFILTER --'+Constants.SIZEFILTER );
  256. // case when user do filter by size
  257. if(strFilterBy == Constants.SIZEFILTER )
  258. {
  259. if(!string.isEmpty(Searchtext))
  260. {
  261. if(Integer.valueOf(Searchtext) == Integer.valueOf(pw.total))
  262. {
  263. lstProdWrapper.add(pw);
  264. }
  265. }
  266. else
  267. {
  268. lstProdWrapper.add(pw);
  269. }
  270.  
  271. }
  272. else
  273. {
  274. lstProdWrapper.add(pw);
  275. }
  276. }
  277. lstProdWrapper.sort();
  278. }
  279.  
  280. public Forecast__c createForecast(gii__ProductInventory__c proInv, Sales_Year__c syear)
  281. {
  282. for(Forecast__c f : allForecast)
  283. {
  284. if(f.Product_Inventory__c == proInv.id && f.Sales_Year__c == syear.id)
  285. return f;
  286. }
  287. Forecast__c fore = new Forecast__c();
  288. fore.name = proInv.gii__Product__r.name + '-' + proInv.name + '-' + syear.name;
  289. fore.Product_Inventory__c = proInv.id;
  290. fore.Sales_Year__c = syear.id;
  291. fore.Warehouse__c = proInv.gii__Warehouse__c;
  292. fore.Product_Reference__c = proInv.gii__Product__c;
  293. insert fore;
  294. return fore;
  295. }
  296.  
  297. public PageReference save()
  298. {
  299. List<Forecast_Detail__c> lstChild = new List<Forecast_Detail__c>();
  300. for(ProductWrapper objProdWrap : lstProdWrapper)
  301. {
  302. for(integer i=0;i<12;i++)
  303. {
  304. if(objProdWrap.monthQty.get(allMonths[i]) != null && objProdWrap.monthQty.get(allMonths[i]) != '')
  305. {
  306. if(objProdWrap.oldMonthQty.get(allMonths[i]) == 'xxx')
  307. {
  308. lstChild.add(new Forecast_Detail__c(name=allMonths[i]+'-'+selectSalesYear.name, Month__c=allMonths[i], Quantity__c=Decimal.valueOf(objProdWrap.monthQty.get(allMonths[i])), Forecast__c=objProdWrap.ProForecast.id));
  309. }
  310. else
  311. {
  312. lstChild.add(new Forecast_Detail__c(id=objProdWrap.IdMonthQty.get(allMonths[i]),name=allMonths[i]+'-'+selectSalesYear.name, Month__c=allMonths[i], Quantity__c=Decimal.valueOf(objProdWrap.monthQty.get(allMonths[i])), Forecast__c=objProdWrap.ProForecast.id));
  313. }
  314. }
  315. }
  316. }
  317. upsert lstChild;
  318. createWrapper();
  319. return null;
  320. }
  321.  
  322. public PageReference saveNexit()
  323. {
  324. save();
  325. PageReference pref = new PageReference(Label.Forecast_RetURL);
  326. return pref;
  327. }
  328.  
  329. global class ProductWrapper implements Comparable
  330. {
  331. public string productstyle {get;set;}
  332. public String productSKU{get;set;}
  333. public String productCode{get;set;}
  334. public Forecast__c ProForecast{get;set;}
  335. public String total{get;set;}
  336. public Map<String, String> monthQty{get;set;}
  337. public Map<String, String> oldMonthQty{get;set;}
  338. public Map<String, Id> IdMonthQty{get;set;}
  339. public Map<String, Boolean> cellEdited{get;set;}
  340. public Boolean checked {get;set;}
  341. public gii__ProductInventory__c emp {get;set;}
  342.  
  343. global Integer compareTo(Object compareTo) {
  344. ProductWrapper compareToEmp = (ProductWrapper)compareTo;
  345. if (productstyle == compareToEmp.productstyle) return 0;
  346. if (productstyle > compareToEmp.productstyle) return 1;
  347. return -1;
  348. }
  349. }
  350.  
  351. public Boolean hasNext
  352. {
  353. get
  354. {
  355. return (PageNumber+1)<NoOfPages;
  356. }
  357. set;
  358. }
  359.  
  360. public Boolean hasPrevious
  361. {
  362. get
  363. {
  364. return PageNumber>0;
  365. }
  366. set;
  367. }
  368.  
  369.  
  370. public void previous()
  371. {
  372. PageNumber--;
  373. createWrapper();
  374. }
  375.  
  376. public void next()
  377. {
  378. PageNumber++;
  379. createWrapper();
  380. }
  381.  
  382. public void del() {
  383. List<gii__ProductInventory__c> listEmpForDel = new List<gii__ProductInventory__c>();
  384. List<ProductWrapper> listTempWrapper = new List<ProductWrapper>();
  385. for(ProductWrapper w : lstProdWrapper) {
  386. if(w.checked) {
  387. listEmpForDel.add(w.emp);
  388. } else {
  389. listTempWrapper.add(w);
  390. }
  391. }
  392. System.debug('++++++++++++Size++++++++++++++++++ :'+listEmpForDel.size());
  393. System.debug('+++++++++++++++++++++++++++++++++++++++'+listTempWrapper+'n');
  394. if(listEmpForDel.size() > 0) {
  395. delete listEmpForDel;
  396. lstProdWrapper= listTempWrapper;
  397. } else {
  398. ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Select atleast one Account to delete'));
  399. }
  400. }
  401.  
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement