PDF Report sample application with Page Header , Group Header, Page Footeraaa

Please suggest changes / or use if found good
/*
The goal of this application is to print a report with Header & set in the Layout/Interface and a body
The body will will have group head. This group head has to be printed whenever a new group is encountered
The lines per page is 50
Group header count is 5
The main point is to avoid printing a group header after 45lines are printed.
For this the logic to check as follows

  1. $lines_printed --> No of detail lines printed

  2. $header_printed_count --> No of times header printed in a page

  3. $total_printed_lines --> Total no. of lines printed which is lines_printed + ($header_printed_count *5)
    NOTE : ($header_printed_count *5) --> there are 5 lines per header, so no of times header printed * 5 givs lines used for header

  4. $totallines_with_new_header = $total_printed_lines +5; This is to test if by printing an header , if it will exceed the page limit of 50 lines per page.
    NOTE : Automatic PageBreak is set to off in the Settings
    -*/

     		$y =0;// sc_pdf_get_y();
     	// [prevBatchNo] is a global Variable set to "START" at onApplicationInit event	
     	// Resetting of variables have to be done conditionally
     		
     	if ([prevBatchNo] == "START"){
     		$errorCount = 0;
     		$currOdmSeqNo = 0;
     		
     		
     		$lines_printed = 0;
     		$print_header = 0;
     		$header_printed_count= 0;
     	} //if ([prevBatchNo] == "START")
    
     	
     	if ([prevBatchNo] != trim({operationmaster.oBatchNumber})){
     		$print_header = 1;
     		//$total_printed_lines=0;
     		}
     	if ($total_printed_lines >=50){
     		$print_header = 1;
     		$lines_printed = 0;
     		
     		sc_pdf_add_page("Portrait");
     		$total_printed_lines=0;
     		$header_printed_count = 0;
     		}
     	if ($print_header ==1){
     		
     		$totallines_with_new_header = $total_printed_lines +5;
     		if ($totallines_with_new_header >=50){
     				
     				sc_pdf_add_page("Portrait");		
     				$lines_printed = 0;
     				$total_printed_lines=0;
     				
     				$header_printed_count= 0;
     			} //if ($totallines_with_new_header >=45) {
     		
     		$itemNo = (trim({operationmaster.oItemNumber}));
     		$prevItemNo = (trim({operationmaster.oprevItemNumber}));
     		
     		// Date & Batch
     		$y = sc_pdf_get_y();
     		
     		// PRINT LINE AFTER SPACING. the sc_pdf_ln line break is important followe by get_y
     		sc_pdf_ln(3);
     		$y = sc_pdf_get_y();
     		sc_pdf_line(5,$y,205,$y);
     		$y = sc_pdf_get_y();
     		sc_pdf_set_xy(5,$y,true); //position somewhere on your PDF
     		
     		sc_pdf_write(0,trim("DateTime")); // the 0 is for linespacing
     		sc_pdf_set_xy(30,$y,true); 
     		sc_pdf_write(0,trim({operationmaster.oDateTime})); 
     		sc_pdf_set_xy(110,$y,true); //position on Report 
     		sc_pdf_write(0,trim("Batch No"));
     		sc_pdf_set_xy(130,$y,true); 
     		sc_pdf_write(0,trim({operationmaster.oBatchNumber})); 
     		sc_pdf_ln(4); //$lines_printed = 21 + 4= 25  25 -- 8 + 4
     		
     		// Item & Prev Item
     		$y = sc_pdf_get_y();
     		sc_pdf_ln(2); 
     		$y = sc_pdf_get_y();
     		sc_pdf_set_xy(5,$y,true); //Location on Report where the next command is printed
     		sc_pdf_write(0,"Item Number "); 
     		sc_pdf_set_xy(30,$y,true); //position somewhere on your PDF 
     		//sc_pdf_write(0,$itemNo); 
     		sc_pdf_write(0,trim({operationmaster.oItemNumber})); 
     		sc_pdf_set_xy(60,$y,true); //Location on Report where the next command is printed 
     		sc_pdf_write(0,"Prev Item No");
     		sc_pdf_set_xy(86,$y,true);  
     		//sc_pdf_write(0,$prevItemNo);
     		sc_pdf_write(0,trim({operationmaster.oprevItemNumber})); 
     		sc_pdf_set_xy(110,$y,true);  
     		sc_pdf_write(0,"PO. No");
     		sc_pdf_set_xy(130,$y,true); 
     		//sc_pdf_write(0,$prevItemNo);
     		sc_pdf_write(0,trim({operationmaster.oPONumber})); 
     		[prevBatchNo] = trim({operationmaster.oBatchNumber});
     		if ($currOdmSeqNo != (int){operationdetails.odmSequenceID}){
     			$currOdmSeqNo = (int){operationdetails.odmSequenceID};
     			$errorCount = 0;
     		}
     		
     		
     		// Detail Header Emp ID, Operation, Loc ID, Loc Name, Result Description
     		$y = sc_pdf_get_y();
     		sc_pdf_ln(5); 
     		$y = sc_pdf_get_y();
     		sc_pdf_set_xy(5,$y,true); 
     		sc_pdf_write(0,"EMP ID");
     	
     		sc_pdf_set_xy(26,$y,true); 
     		sc_pdf_write(0,"Operation ");
     		sc_pdf_set_xy(55,$y,true);
     		 sc_pdf_write(0,"Loc ID");
     		sc_pdf_set_xy(70,$y,true); 
     		sc_pdf_write(0,"Location Name ");
     		sc_pdf_set_xy(145,$y,true); 
     		sc_pdf_write(0,"Result Description"); 
     		sc_pdf_set_xy(178,$y,true); 
     		sc_pdf_write(0,"Err Cnt "); 
     		// PRINT LINE AFTER SPACING. the sc_pdf_ln line break is important followe by get_y
     		sc_pdf_ln(8);
     		$y = sc_pdf_get_y();
     		sc_pdf_line(5,$y,205,$y);
     		$y = sc_pdf_get_y();
     		sc_pdf_ln(1);  
     		$y = sc_pdf_get_y();
     		$header_printed_count=$header_printed_count+ 1;
     		$print_header=0;
     		
     	} //if ([prevBatchNo] != trim({operationmaster.oBatchNumber})){
     			
     			
     			
     	// DETAIL PRINT SECTION
     	
     			
     			
     	//$y = sc_pdf_get_y();
     	//$y = $y+2;
     	$y = sc_pdf_get_y();
     		//sc_pdf_ln(5);  
     		//$y = sc_pdf_get_y();
     	$errorCount = $errorCount +1;
     	sc_pdf_set_xy(5,$y,true); //position on your PDF 
     	sc_pdf_write(0,trim({operationdetails.odEmployeeID}));
     	sc_pdf_set_xy(26,$y,true);  
     	sc_pdf_write(0,trim({operationdetails.odOperationName}));
     	sc_pdf_set_xy(55,$y,true);  
     	 sc_pdf_write(0,trim({operationdetails.odLocationID}));
     	sc_pdf_set_xy(70,$y,true); 
     	sc_pdf_write(0,trim({operationdetails.odLocationName}));
     	sc_pdf_set_xy(125,$y,true);  
     	
     	sc_pdf_set_xy(145,$y,true); 
     	sc_pdf_write(0,substr(trim({operationdetails.odLocationScanIntervalResult}),0,20));  
     	sc_pdf_set_xy(180,$y,true); 
     	//sc_pdf_write(0,$errorCount); //commented for debug purpose. Uncomment latter
     	//sc_pdf_set_xy(189,$y,true);  // commented for debug but not needed for main run
     	//sc_pdf_write(0,trim({operationdetails.odmSequenceID}));//commented for debug not needed for main run
     	
     	$lines_printed = $lines_printed+1;
     	$total_printed_lines =  ($header_printed_count *5) + $lines_printed; //header can be printed multiple times
     	// So no of lines used for header is $header_printed_count *5
     	// Following $lines_header_count and following write is for debug 
     	$lines_header_count = "NA";
     	$lines_header_count = strval($line_printed) ." / " .strval($header_printed_count) ." / " 										.strval($total_printed_lines);
     	sc_pdf_write(0,$lines_header_count);
     	 
     	
     	//if ($total_printed_lines >=45) {
     	//	$print_header = 1;
     	//	}
     	sc_pdf_write(0,$lines_printed);
     	sc_pdf_set_xy(185,$y,true); 
     	sc_pdf_write(0,"/");
     	sc_pdf_set_xy(189,$y,true); 
     	sc_pdf_write(0,$total_printed_lines);*/