2017-06-26 18:36:48 +01:00
import { Component , OnInit } from '@angular/core' ;
import { Validators , FormBuilder , FormGroup } from '@angular/forms' ;
import { Http , Response } from '@angular/http' ;
import { ApiService } from '../providers/api-service' ;
2017-08-30 16:09:57 +01:00
import { OrgTableComponent } from '../shared/org-table.component' ;
2017-08-29 18:15:47 +01:00
import * as moment from 'moment' ;
2017-06-26 18:36:48 +01:00
import 'rxjs/add/operator/map' ;
@Component ( {
templateUrl : 'add-data.component.html' ,
providers : [ ApiService ]
} )
export class AddDataComponent {
2017-06-29 16:33:11 +01:00
payrollForm : FormGroup ;
singleSupplierForm : FormGroup ;
employeeForm : FormGroup ;
2017-08-29 18:15:47 +01:00
transactionForm : FormGroup ;
2017-08-25 12:18:14 +01:00
payrollFormStatus : any ;
singleSupplierFormStatus : any ;
employeeFormStatus : any ;
2017-08-29 18:15:47 +01:00
transactionFormStatus : any ;
transactionFormStatusError : string = 'Error received, please try again.' ;
2017-08-31 18:44:17 +01:00
accountType : any ;
2017-08-30 16:09:57 +01:00
2017-08-29 18:15:47 +01:00
submitOrg = {
name : '' ,
street_name : '' ,
town : '' ,
postcode : '' ,
} ;
organisationId : number ;
organisationTown : string ;
organisationPostcode : string ;
amount : number ;
transactionAdditionType = 1 ;
storeList ;
showAddStore = false ;
submitReceipt = false ;
transactionFormInvalid = true ;
myDate : any ;
minDate : any ;
2017-06-26 18:36:48 +01:00
2017-06-29 16:33:11 +01:00
constructor (
private http : Http ,
private formBuilder : FormBuilder ,
private api : ApiService ,
2017-08-30 16:09:57 +01:00
) {
2017-08-29 18:15:47 +01:00
this . payrollForm = this . formBuilder . group ( {
2017-06-29 16:33:11 +01:00
entryperiod : [ '' , [ Validators . required ] ] ,
employeeamount : [ '' , [ Validators . required ] ] ,
localemployeeamount : [ '' , [ Validators . required ] ] ,
grosspayroll : [ '' , [ Validators . required ] ] ,
2017-09-08 12:16:43 +01:00
payrollincometax : [ '' , [ Validators . required ] ] ,
payrollemployeeni : [ '' , [ Validators . required ] ] ,
payrollemployerni : [ '' , [ Validators . required ] ] ,
payrolltotalpension : [ '' , [ Validators . required ] ] ,
payrollotherbenefit : [ '' , [ Validators . required ] ] ,
2017-06-29 16:33:11 +01:00
} ) ;
this . singleSupplierForm = this . formBuilder . group ( {
2017-07-03 17:20:48 +01:00
entryperiod : [ '' , [ Validators . required ] ] ,
2017-09-08 12:16:43 +01:00
supplierbusinessname : [ '' , [ Validators . required ] ] ,
postcode : [ '' , [ Validators . required ] ] ,
monthlyspend : [ '' , [ Validators . required ] ] ,
2017-06-29 16:33:11 +01:00
} ) ;
this . employeeForm = this . formBuilder . group ( {
2017-07-03 17:20:48 +01:00
entryperiod : [ '' , [ Validators . required ] ] ,
2017-09-08 12:16:43 +01:00
employeeno : [ '' , [ Validators . required ] ] ,
employeeincometax : [ '' , [ Validators . required ] ] ,
employeegrosswage : [ '' , [ Validators . required ] ] ,
employeeni : [ '' , [ Validators . required ] ] ,
employeepension : [ '' , [ Validators . required ] ] ,
employeeotherbenefit : [ '' , [ Validators . required ] ] ,
2017-06-29 16:33:11 +01:00
} ) ;
2017-08-29 18:15:47 +01:00
this . myDate = moment ( ) . format ( 'YYYY-MM-DD[T]HH:mm' ) ;
// this.myDate = new Date().toISOString().slice(0, 16);
}
2017-08-30 16:09:57 +01:00
2017-08-29 18:15:47 +01:00
ngOnInit ( ) : void {
this . getMinDate ( ) ;
2017-08-31 18:44:17 +01:00
this . accountType = localStorage . getItem ( 'usertype' ) ;
2017-08-29 18:15:47 +01:00
}
getMinDate ( ) {
// gets the April 1st date of the current year
let aprilDate = moment ( ) . month ( 3 ) . date ( 1 ) ;
let now = moment ( ) ;
// Checks if current time is before April 1st, if so returns true
let beforeApril = now . isBefore ( aprilDate ) ;
if ( beforeApril == true ) {
this . minDate = aprilDate . subtract ( 2 , 'years' ) . format ( 'YYYY-MM-DD' ) ;
} else {
this . minDate = aprilDate . subtract ( 1 , 'years' ) . format ( 'YYYY-MM-DD' ) ;
}
}
2017-08-30 16:09:57 +01:00
2017-08-29 18:15:47 +01:00
initializeItems() {
// Dont bother searching for an empty or undefined string
if ( this . submitOrg . name == '' ) {
return ;
}
var searchData = {
search_name : this.submitOrg.name ,
} ;
this . api . search ( searchData ) . subscribe (
data = > {
if ( data . validated . length > 0 ) {
this . storeList = data . validated ;
this . showAddStore = false ;
this . transactionAdditionType = 1 ;
} else {
this . storeList = data . unvalidated ;
this . showAddStore = false ;
this . transactionAdditionType = 2 ;
}
// handle the case when the storelist is empty
if ( this . storeList . length < 1 ) {
this . storeList = null ;
this . showAddStore = true ;
this . transactionAdditionType = 3 ;
}
} ,
error = > {
console . log ( error ) ;
}
) ;
}
// if user select a item from the list
addStore ( store ) {
this . submitOrg = store ;
this . transactionFormValidate ( ) ;
this . organisationId = store . id ;
}
// search for store
organisationSearch ( ev ) {
// Reset items back to all of the items
this . initializeItems ( ) ;
// set val to the value of the searchbar
let val = ev . target . value ;
// Filter the store list so search seems quicker
if ( val && val . trim ( ) != '' && this . storeList != null ) {
this . storeList = this . storeList . filter (
( item ) = > {
return ( item . name . toLowerCase ( ) . indexOf ( val . toLowerCase ( ) ) > - 1 ) ;
}
)
}
// if nothing is found
if ( ! this . storeList === null ) {
// display add new store button
this . showAddStore = true ;
}
}
transactionFormValidate() {
2017-09-08 11:54:34 +01:00
if ( this . submitOrg . name . length == 0 &&
2017-08-29 18:15:47 +01:00
this . amount == 0 ) {
this . transactionFormInvalid = true ;
} else {
this . transactionFormInvalid = false ;
}
}
public postTransaction() {
var myParams : any ;
let purchaseTime : string ;
purchaseTime = moment ( this . myDate , 'YYYY-MM-DD[T]HH:mm' ) . local ( ) . format ( 'YYYY-MM-DD[T]HH:mm:ss.SSSZ' ) ;
switch ( this . transactionAdditionType ) {
case 1 :
myParams = {
transaction_type : this.transactionAdditionType ,
transaction_value : this.amount ,
purchase_time : purchaseTime ,
organisation_id : this.organisationId ,
} ;
break ;
case 2 :
myParams = {
transaction_type : this.transactionAdditionType ,
transaction_value : this.amount ,
purchase_time : purchaseTime ,
organisation_id : this.organisationId ,
} ;
break ;
case 3 :
myParams = {
transaction_type : this.transactionAdditionType ,
transaction_value : this.amount ,
purchase_time : purchaseTime ,
organisation_name : this.submitOrg.name ,
street_name : this.submitOrg.street_name ,
town : this.submitOrg.town ,
postcode : this.submitOrg.postcode ,
} ;
break ;
}
/******************************/
this . api
. upload ( myParams )
. subscribe (
result = > {
if ( result . success == true ) {
console . log ( 'Successful Upload' ) ;
console . log ( result ) ;
this . transactionFormStatus = "success" ;
console . log ( this . transactionFormStatus ) ;
this . resetForm ( ) ;
} else {
console . log ( 'Upload Error' ) ;
this . transactionFormStatusError = JSON . stringify ( result . status ) + 'Error, ' + JSON . stringify ( result . message ) ;
this . transactionFormStatus = "send_failed" ;
console . log ( this . transactionFormStatus ) ;
}
} ,
error = > {
console . log ( 'Upload Error' ) ;
console . log ( error ) ;
try {
2017-08-30 16:09:57 +01:00
console . log ( error . error ) ;
let jsonError = error . json ( ) ;
console . log ( "boop" ) ;
this . transactionFormStatusError = '"' + jsonError . error + '" Error, ' + jsonError . message ;
2017-08-29 18:15:47 +01:00
} catch ( e ) {
this . transactionFormStatusError = 'There was a server error, please try again later.' ;
}
this . transactionFormStatus = "send_failed" ;
console . log ( this . transactionFormStatus ) ;
}
) ;
}
private resetForm() {
this . submitOrg = {
name : '' ,
street_name : '' ,
town : '' ,
postcode : '' ,
} ;
this . storeList = null ;
this . amount = null ;
this . transactionFormInvalid = true ;
2017-06-29 16:33:11 +01:00
}
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
onSubmitPayroll() {
console . log ( this . payrollForm . value ) ;
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
this . api
2017-09-08 11:54:34 +01:00
. orgPayroll ( this . payrollForm . value )
2017-06-29 16:33:11 +01:00
. subscribe (
result = > {
console . log ( 'data submitted!' ) ;
2017-08-25 12:18:14 +01:00
this . payrollFormStatus = "success" ;
console . log ( this . payrollFormStatus ) ;
2017-06-29 16:33:11 +01:00
} ,
error = > {
console . log ( error . _body ) ;
2017-08-25 12:18:14 +01:00
this . payrollFormStatus = "send_failed" ;
console . log ( this . payrollFormStatus ) ;
2017-06-29 16:33:11 +01:00
}
) ;
}
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
onSubmitSingleSupplier() {
console . log ( this . singleSupplierForm . value ) ;
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
this . api
2017-09-08 11:54:34 +01:00
. orgSupplier ( this . singleSupplierForm . value )
2017-06-29 16:33:11 +01:00
. subscribe (
result = > {
console . log ( 'data submitted!' ) ;
2017-08-25 12:18:14 +01:00
this . singleSupplierFormStatus = "success" ;
console . log ( this . singleSupplierFormStatus ) ;
2017-06-29 16:33:11 +01:00
} ,
error = > {
console . log ( error . _body ) ;
2017-08-25 12:18:14 +01:00
this . singleSupplierFormStatus = "send_failed" ;
console . log ( this . singleSupplierFormStatus ) ;
2017-06-29 16:33:11 +01:00
}
) ;
}
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
onSubmitEmployee() {
console . log ( this . employeeForm . value ) ;
2017-08-30 16:09:57 +01:00
2017-06-29 16:33:11 +01:00
this . api
2017-09-08 11:54:34 +01:00
. orgEmployee ( this . employeeForm . value )
2017-06-29 16:33:11 +01:00
. subscribe (
result = > {
console . log ( 'data submitted!' ) ;
2017-08-25 12:18:14 +01:00
this . employeeFormStatus = "success" ;
console . log ( this . employeeFormStatus ) ;
2017-06-29 16:33:11 +01:00
} ,
error = > {
console . log ( error . _body ) ;
2017-08-25 12:18:14 +01:00
this . employeeFormStatus = "send_failed" ;
console . log ( this . employeeFormStatus ) ;
2017-06-29 16:33:11 +01:00
}
) ;
}
2017-06-26 18:36:48 +01:00
}