Skip to content

Commit

Permalink
New Payroll Report PDF #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Trever authored and Trever committed Apr 20, 2017
1 parent 3a55334 commit cf217e5
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 129 deletions.
32 changes: 28 additions & 4 deletions inSparkle.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,38 @@
<Bucket
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "inSparkle/PayrollReportHTML.swift"
timestampString = "514397109.77003"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "90"
endingLineNumber = "90"
landmarkName = "exportHTMLContentToPDF(HTMLContent:printFormatter:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "inSparkle/PayrollReportHTML.swift"
timestampString = "514397109.77003"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "150"
endingLineNumber = "150"
landmarkName = "presentPDF(fileName:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
3 changes: 0 additions & 3 deletions inSparkle/AddNewExpenseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,6 @@ class AddNewExpenseViewController: UIViewController, UIGestureRecognizerDelegate
}

func savingAnimation() -> NVActivityIndicatorView {
let x = (self.view.frame.size.width / 2)
let y = (self.view.frame.size.height / 2)

let background = UIView()
background.backgroundColor = UIColor.black
background.frame = CGRect(x: 0, y: 0, width: 247, height: 247)
Expand Down
18 changes: 9 additions & 9 deletions inSparkle/Admin.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Pdb-5y-vV0">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="Pdb-5y-vV0">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand All @@ -24,11 +24,11 @@
<rect key="frame" x="0.0" y="35" width="375" height="48"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="BKF-Ua-CqZ" id="iRC-er-g3A">
<rect key="frame" x="0.0" y="0.0" width="342" height="47"/>
<rect key="frame" x="0.0" y="0.0" width="342" height="47.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Reports" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="qNH-20-oK1">
<rect key="frame" x="60" y="0.0" width="280" height="47"/>
<rect key="frame" x="60" y="0.0" width="280" height="47.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -48,11 +48,11 @@
<rect key="frame" x="0.0" y="83" width="375" height="48"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="fPs-g3-WBC" id="vmp-eo-ZUE">
<rect key="frame" x="0.0" y="0.0" width="342" height="47"/>
<rect key="frame" x="0.0" y="0.0" width="342" height="47.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text=" Time Card Management" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="fBk-yv-p7C">
<rect key="frame" x="64" y="0.0" width="276" height="47"/>
<rect key="frame" x="64" y="0.0" width="276" height="47.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -72,11 +72,11 @@
<rect key="frame" x="0.0" y="131" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="O7V-CY-7Ty" id="KLB-M7-d1t">
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="342" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Time Away Requests" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="cqb-yL-oHE">
<rect key="frame" x="64" y="0.0" width="276" height="43"/>
<rect key="frame" x="64" y="0.0" width="276" height="43.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -96,11 +96,11 @@
<rect key="frame" x="0.0" y="175" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="V83-nq-2Rd" id="bAI-1Z-TRe">
<rect key="frame" x="0.0" y="0.0" width="342" height="43"/>
<rect key="frame" x="0.0" y="0.0" width="342" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Manage Users and Employees" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="EJz-6Q-L74">
<rect key="frame" x="64" y="0.0" width="276" height="43"/>
<rect key="frame" x="64" y="0.0" width="276" height="43.5"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down
23 changes: 23 additions & 0 deletions inSparkle/EmployeePayrollReportModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// EmployeePayrollReportModel.swift
// inSparkle
//
// Created by Trever on 4/19/17.
// Copyright © 2017 Sparkle Pools. All rights reserved.
//

import UIKit

class EmployeePayrollReportModel: NSObject {

var employeeName : String!

var standardHours : String!

var overtimeHours : String!

var vacationHours : String!

var total : String!

}
8 changes: 7 additions & 1 deletion inSparkle/ExpenseDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class ExpenseDetailViewController: UIViewController, UIGestureRecognizerDelegate
if error == nil {
self.additionalAttachments = addtlAttch as! [ExpenseAdditionalAttachments]
self.attachmentCollectionView.reloadData()
self.stopLoadingIndicator()
}
})
}
Expand Down Expand Up @@ -454,6 +453,13 @@ extension ExpenseDetailViewController : UICollectionViewDelegate, UICollectionVi
return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

if indexPath.item == collectionView.indexPathsForVisibleItems.last?.item {
self.stopLoadingIndicator()
}

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
var temp = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
Expand Down
35 changes: 0 additions & 35 deletions inSparkle/PayrollPDFGenerate.storyboard

This file was deleted.

191 changes: 191 additions & 0 deletions inSparkle/PayrollReportHTML.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
//
// PayrollReportHTML.swift
// inSparkle
//
// Created by Trever on 4/19/17.
// Copyright © 2017 Sparkle Pools. All rights reserved.
//

import Foundation
import UIKit
import NVActivityIndicatorView

class PayrollReportHTML : NSObject {

let pathToHTMLTemplate = Bundle.main.path(forResource: "report", ofType: "html")

let pathToEmployeeLine = Bundle.main.path(forResource: "employeeline", ofType: "html")

let logoImage = Bundle.main.path(forResource: "payrollreportlogo", ofType: "png")

var pdfFileName : String!

override init() {
super.init()
}

func renderInvoice(periodDates : String, employees : [EmployeePayrollReportModel]) -> String! {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .medium
dateFormatter.timeZone = SparkleTimeZone.timeZone

let date = dateFormatter.string(from: Date())

print(employees.count)

do {
var HTMLContent = try String(contentsOfFile: pathToHTMLTemplate!)

HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: logoImage!)

HTMLContent = HTMLContent.replacingOccurrences(of: "#PAY_PERIOD_DATES#", with: periodDates)

HTMLContent = HTMLContent.replacingOccurrences(of: "#RUN_DATE#", with: date)

var allLines = ""

for emp in employees {

var itemHTMLContent : String! = try String(contentsOfFile: pathToEmployeeLine!)

itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#EMPLOYEE#", with: emp.employeeName)

itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#STD_HOURS#", with: emp.standardHours)

itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#OT_HOURS#", with: emp.overtimeHours)

itemHTMLContent = itemHTMLContent.replacingOccurrences(of: "#TTL_HOURS#", with: emp.total)

allLines += itemHTMLContent

}

HTMLContent = HTMLContent.replacingOccurrences(of: "#LINES#", with: allLines)

return HTMLContent
} catch {

}

return nil

}

func exportHTMLContentToPDF(HTMLContent : String, printFormatter : UIViewPrintFormatter) {
let printPageRenderer = printPayrollToPDF()


// let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)

let pdfData = drawPDFUsiingPrintPageRenderer(printPageRenderer: printPageRenderer)


let directory = NSTemporaryDirectory()
let fileName = NSUUID().uuidString

pdfFileName = directory + fileName + ".pdf"

pdfData.write(toFile: pdfFileName, atomically: true)

thePayrollView.presentPDF(fileName: pdfFileName)

}


func drawPDFUsiingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData {
let data = NSMutableData()

UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)

UIGraphicsBeginPDFPage()

printPageRenderer.drawPage(at: 0, in: UIGraphicsGetPDFContextBounds())

UIGraphicsEndPDFContext()

return data
}


}

var thePayrollView : PayrollWebPreview!

class PayrollWebPreview : UIViewController, UIWebViewDelegate, UIDocumentInteractionControllerDelegate {

@IBOutlet var webView : UIWebView!
var payrollHTML : PayrollReportHTML!

var periodDates : String!
var employees : [EmployeePayrollReportModel]!

var HTML : String!
var docController: UIDocumentInteractionController?
var loadingUI : NVActivityIndicatorView!

override func viewDidAppear(_ animated: Bool) {
webView.delegate = self
createReportAsHTML()
}

func createReportAsHTML() {
payrollHTML = PayrollReportHTML()
if let payrollHTML2 = payrollHTML.renderInvoice(periodDates: periodDates, employees: employees) {
self.webView.loadHTMLString(payrollHTML2, baseURL: URL(string: payrollHTML.pathToHTMLTemplate!))
HTML = payrollHTML2
}
}

func webViewDidFinishLoad(_ webView: UIWebView) {
thePayrollView = self
PayrollReportHTML().exportHTMLContentToPDF(HTMLContent: HTML, printFormatter: webView.viewPrintFormatter())
}

func presentPDF(fileName : String) {
var docController: UIDocumentInteractionController?
docController = UIDocumentInteractionController(url: URL(fileURLWithPath: fileName))
docController!.delegate = self
docController!.presentPreview(animated: true)
}

func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
return self.view.frame
}

func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
let viewController: UIViewController = UIViewController()
self.present(viewController, animated: true, completion: nil)
return viewController
}

func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
self.dismiss(animated: true , completion: nil)
self.dismiss(animated: true) {
NotificationCenter.default.post(name: Notification.string(name: "dismissRunTimeReport"), object: nil)
}
}
}

class printPayrollToPDF : UIPrintPageRenderer {

let A4PageWidth: CGFloat = 595.2

let A4PageHeight: CGFloat = 841.8

override init() {
super.init()

// Specify the frame of the A4 page.
let pageFrame = CGRect(x: 0.0, y: 0.0, width: A4PageWidth, height: A4PageHeight)

// Set the page frame.
self.setValue(NSValue(cgRect: pageFrame), forKey: "paperRect")

// Set the horizontal and vertical insets (that's optional).
self.setValue(NSValue(cgRect: pageFrame.insetBy(dx: 10.0, dy: 10.0)), forKey: "printableRect")

}

}
Loading

0 comments on commit cf217e5

Please sign in to comment.