2017-06-26 17:36:48 +00:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
|
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
|
|
|
|
import { ApiService } from '../providers/api-service';
|
2017-08-30 15:09:57 +00:00
|
|
|
import { OrgTableComponent } from '../shared/org-table.component';
|
2017-08-29 17:15:47 +00:00
|
|
|
import * as moment from 'moment';
|
2017-06-26 17:36:48 +00:00
|
|
|
import 'rxjs/add/operator/map';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: 'add-data.component.html',
|
|
|
|
})
|
2017-09-19 13:53:25 +00:00
|
|
|
export class AddDataComponent implements OnInit {
|
2017-06-29 15:33:11 +00:00
|
|
|
payrollForm: FormGroup;
|
|
|
|
singleSupplierForm: FormGroup;
|
|
|
|
employeeForm: FormGroup;
|
2017-08-29 17:15:47 +00:00
|
|
|
transactionForm: FormGroup;
|
2017-08-25 11:18:14 +00:00
|
|
|
payrollFormStatus: any;
|
|
|
|
singleSupplierFormStatus: any;
|
|
|
|
employeeFormStatus: any;
|
2017-08-29 17:15:47 +00:00
|
|
|
transactionFormStatus: any;
|
2017-09-19 13:53:25 +00:00
|
|
|
transactionFormStatusError = 'Error received, please try again.';
|
2017-08-31 17:44:17 +00:00
|
|
|
accountType: any;
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-08-29 17:15:47 +00:00
|
|
|
submitOrg = {
|
|
|
|
name: '',
|
|
|
|
street_name: '',
|
|
|
|
town: '',
|
|
|
|
postcode: '',
|
|
|
|
};
|
|
|
|
organisationId: number;
|
|
|
|
organisationTown: string;
|
|
|
|
organisationPostcode: string;
|
|
|
|
amount: number;
|
2018-01-15 16:20:51 +00:00
|
|
|
categoryId: number;
|
2017-08-29 17:15:47 +00:00
|
|
|
transactionAdditionType = 1;
|
2017-10-20 12:21:49 +00:00
|
|
|
storeList = [];
|
2017-08-29 17:15:47 +00:00
|
|
|
showAddStore = false;
|
|
|
|
submitReceipt = false;
|
|
|
|
transactionFormInvalid = true;
|
|
|
|
myDate: any;
|
|
|
|
minDate: any;
|
2018-01-15 16:20:51 +00:00
|
|
|
categoryIdList: number[] = [];
|
|
|
|
categoryNameList: string[] = [];
|
2017-06-26 17:36:48 +00:00
|
|
|
|
2017-06-29 15:33:11 +00:00
|
|
|
constructor(
|
2017-09-19 13:53:25 +00:00
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private api: ApiService,
|
|
|
|
) {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.payrollForm = this.formBuilder.group({
|
2017-09-19 13:53:25 +00:00
|
|
|
entry_period: ['', [Validators.required]],
|
|
|
|
employee_amount: ['', [Validators.required]],
|
|
|
|
local_employee_amount: ['', [Validators.required]],
|
|
|
|
gross_payroll: ['', [Validators.required]],
|
2017-09-08 11:55:42 +00:00
|
|
|
payroll_income_tax: ['', [Validators.required]],
|
|
|
|
payroll_employee_ni: ['', [Validators.required]],
|
|
|
|
payroll_employer_ni: ['', [Validators.required]],
|
|
|
|
payroll_total_pension: ['', [Validators.required]],
|
|
|
|
payroll_other_benefit: ['', [Validators.required]],
|
2017-09-19 13:53:25 +00:00
|
|
|
});
|
2017-06-29 15:33:11 +00:00
|
|
|
this.employeeForm = this.formBuilder.group({
|
2017-09-08 11:55:42 +00:00
|
|
|
entry_period: ['', [Validators.required]],
|
|
|
|
employee_no: ['', [Validators.required]],
|
|
|
|
employee_income_tax: ['', [Validators.required]],
|
|
|
|
employee_gross_wage: ['', [Validators.required]],
|
|
|
|
employee_ni: ['', [Validators.required]],
|
|
|
|
employee_pension: ['', [Validators.required]],
|
|
|
|
employee_other_benefit: ['', [Validators.required]],
|
2017-09-19 13:53:25 +00:00
|
|
|
});
|
2017-08-29 17:15:47 +00:00
|
|
|
this.myDate = moment().format('YYYY-MM-DD[T]HH:mm');
|
|
|
|
// this.myDate = new Date().toISOString().slice(0, 16);
|
2018-01-15 15:33:38 +00:00
|
|
|
this.api.categoryList().subscribe(
|
|
|
|
result => {
|
|
|
|
this.categories = result;
|
|
|
|
console.log(this.categories);
|
2018-01-15 16:20:51 +00:00
|
|
|
this.setCategoryList(result.categories);
|
2018-01-15 15:33:38 +00:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log('Retrieval Error');
|
|
|
|
console.log( error._body );
|
|
|
|
}
|
|
|
|
);
|
2017-08-29 17:15:47 +00:00
|
|
|
}
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-08-29 17:15:47 +00:00
|
|
|
ngOnInit(): void {
|
|
|
|
this.getMinDate();
|
2017-08-31 17:44:17 +00:00
|
|
|
this.accountType = localStorage.getItem('usertype');
|
2017-08-29 17:15:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 16:20:51 +00:00
|
|
|
private setCategoryList(data: any) {
|
|
|
|
this.categoryIdList = Object.keys(data.ids).map(key => data.ids[key]);
|
|
|
|
this.categoryNameList = Object.keys(data.names).map(key => data.names[key]);
|
|
|
|
}
|
|
|
|
|
2017-09-19 13:53:25 +00:00
|
|
|
getMinDate() {
|
2017-08-29 17:15:47 +00:00
|
|
|
// gets the April 1st date of the current year
|
2017-09-19 13:53:25 +00:00
|
|
|
const aprilDate = moment().month(3).date(1);
|
|
|
|
const now = moment();
|
2017-08-29 17:15:47 +00:00
|
|
|
// Checks if current time is before April 1st, if so returns true
|
2017-09-19 13:53:25 +00:00
|
|
|
const beforeApril = now.isBefore(aprilDate);
|
|
|
|
if ( beforeApril === true ) {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.minDate = aprilDate.subtract(2, 'years').format('YYYY-MM-DD');
|
|
|
|
} else {
|
|
|
|
this.minDate = aprilDate.subtract(1, 'years').format('YYYY-MM-DD');
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-08-29 17:15:47 +00:00
|
|
|
initializeItems() {
|
|
|
|
// Dont bother searching for an empty or undefined string
|
2017-09-19 13:53:25 +00:00
|
|
|
if ( this.submitOrg.name === '' ) {
|
2017-08-29 17:15:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-19 13:53:25 +00:00
|
|
|
const searchData = {
|
2017-08-29 17:15:47 +00:00
|
|
|
search_name: this.submitOrg.name,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.api.search(searchData).subscribe(
|
|
|
|
data => {
|
2017-09-19 13:53:25 +00:00
|
|
|
if (data.validated.length > 0) {
|
2017-08-29 17:15:47 +00:00
|
|
|
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
|
2017-09-19 13:53:25 +00:00
|
|
|
if (this.storeList.length < 1) {
|
2017-10-20 12:21:49 +00:00
|
|
|
this.storeList = [];
|
2017-08-29 17:15:47 +00:00
|
|
|
this.showAddStore = true;
|
|
|
|
this.transactionAdditionType = 3;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if user select a item from the list
|
2017-09-19 13:53:25 +00:00
|
|
|
addStore(store) {
|
2017-08-29 17:15:47 +00:00
|
|
|
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
|
2017-09-19 13:53:25 +00:00
|
|
|
const val = ev.target.value;
|
2017-08-29 17:15:47 +00:00
|
|
|
|
|
|
|
// Filter the store list so search seems quicker
|
2017-10-20 12:21:49 +00:00
|
|
|
if (val && val.trim() !== '' && this.storeList.length > 0) {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.storeList = this.storeList.filter(
|
|
|
|
(item) => {
|
|
|
|
return ( item.name.toLowerCase().indexOf( val.toLowerCase() ) > -1 );
|
|
|
|
}
|
2017-09-19 13:53:25 +00:00
|
|
|
);
|
2017-08-29 17:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if nothing is found
|
2017-09-19 13:53:25 +00:00
|
|
|
if (!this.storeList === null) {
|
2017-08-29 17:15:47 +00:00
|
|
|
// display add new store button
|
|
|
|
this.showAddStore = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionFormValidate() {
|
2017-09-19 13:53:25 +00:00
|
|
|
if (this.submitOrg.name.length === 0 ||
|
|
|
|
this.submitOrg.town.length === 0 ||
|
|
|
|
this.amount === 0 ) {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.transactionFormInvalid = true;
|
2017-09-19 13:53:25 +00:00
|
|
|
} else {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.transactionFormInvalid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public postTransaction() {
|
|
|
|
|
2017-09-19 13:53:25 +00:00
|
|
|
let myParams: any;
|
2017-08-29 17:15:47 +00:00
|
|
|
let purchaseTime: string;
|
|
|
|
purchaseTime = moment(this.myDate, 'YYYY-MM-DD[T]HH:mm').local().format('YYYY-MM-DD[T]HH:mm:ss.SSSZ');
|
2017-09-19 13:53:25 +00:00
|
|
|
switch (this.transactionAdditionType) {
|
2017-08-29 17:15:47 +00:00
|
|
|
case 1:
|
|
|
|
myParams = {
|
|
|
|
transaction_type : this.transactionAdditionType,
|
|
|
|
transaction_value : this.amount,
|
|
|
|
purchase_time : purchaseTime,
|
|
|
|
organisation_id : this.organisationId,
|
2018-01-15 16:20:51 +00:00
|
|
|
category : this.categoryId,
|
2017-08-29 17:15:47 +00:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
myParams = {
|
|
|
|
transaction_type : this.transactionAdditionType,
|
|
|
|
transaction_value : this.amount,
|
|
|
|
purchase_time : purchaseTime,
|
|
|
|
organisation_id : this.organisationId,
|
2018-01-15 16:20:51 +00:00
|
|
|
category : this.categoryId,
|
2017-08-29 17:15:47 +00:00
|
|
|
};
|
|
|
|
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,
|
2018-01-15 16:20:51 +00:00
|
|
|
category : this.categoryId,
|
2017-08-29 17:15:47 +00:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/******************************/
|
|
|
|
|
|
|
|
this.api
|
|
|
|
.upload(myParams)
|
|
|
|
.subscribe(
|
|
|
|
result => {
|
2017-09-19 13:53:25 +00:00
|
|
|
if ( result.success === true ) {
|
2017-08-29 17:15:47 +00:00
|
|
|
console.log('Successful Upload');
|
|
|
|
console.log(result);
|
2017-09-19 13:53:25 +00:00
|
|
|
this.transactionFormStatus = 'success';
|
2017-08-29 17:15:47 +00:00
|
|
|
console.log(this.transactionFormStatus);
|
|
|
|
this.resetForm();
|
|
|
|
} else {
|
|
|
|
console.log('Upload Error');
|
|
|
|
this.transactionFormStatusError = JSON.stringify(result.status) + 'Error, ' + JSON.stringify(result.message);
|
2017-09-19 13:53:25 +00:00
|
|
|
this.transactionFormStatus = 'send_failed';
|
2017-08-29 17:15:47 +00:00
|
|
|
console.log(this.transactionFormStatus);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log('Upload Error');
|
|
|
|
console.log(error);
|
|
|
|
try {
|
2017-08-30 15:09:57 +00:00
|
|
|
console.log(error.error);
|
2017-09-19 13:53:25 +00:00
|
|
|
const jsonError = error.json();
|
|
|
|
console.log('boop');
|
2017-08-30 15:09:57 +00:00
|
|
|
this.transactionFormStatusError = '"' + jsonError.error + '" Error, ' + jsonError.message;
|
2017-09-19 13:53:25 +00:00
|
|
|
} catch (e) {
|
2017-08-29 17:15:47 +00:00
|
|
|
this.transactionFormStatusError = 'There was a server error, please try again later.';
|
|
|
|
}
|
2017-09-19 13:53:25 +00:00
|
|
|
this.transactionFormStatus = 'send_failed';
|
2017-08-29 17:15:47 +00:00
|
|
|
console.log(this.transactionFormStatus);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetForm() {
|
|
|
|
this.submitOrg = {
|
|
|
|
name: '',
|
|
|
|
street_name: '',
|
|
|
|
town: '',
|
|
|
|
postcode: '',
|
|
|
|
};
|
2017-11-13 13:05:42 +00:00
|
|
|
this.storeList = [];
|
2017-08-29 17:15:47 +00:00
|
|
|
this.amount = null;
|
|
|
|
this.transactionFormInvalid = true;
|
2017-09-15 15:50:53 +00:00
|
|
|
this.showAddStore = false;
|
2018-01-15 16:20:51 +00:00
|
|
|
this.categoryId = null;
|
2017-06-29 15:33:11 +00:00
|
|
|
}
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-06-29 15:33:11 +00:00
|
|
|
onSubmitPayroll() {
|
2017-09-19 13:53:25 +00:00
|
|
|
console.log(this.payrollForm.value);
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-09-19 13:53:25 +00:00
|
|
|
this.api
|
2017-09-08 10:54:34 +00:00
|
|
|
.orgPayroll(this.payrollForm.value)
|
2017-06-29 15:33:11 +00:00
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
console.log('data submitted!');
|
2017-09-19 13:53:25 +00:00
|
|
|
this.payrollFormStatus = 'success';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.payrollFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log( error._body );
|
2017-09-19 13:53:25 +00:00
|
|
|
this.payrollFormStatus = 'send_failed';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.payrollFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-06-29 15:33:11 +00:00
|
|
|
onSubmitSingleSupplier() {
|
2017-09-19 13:53:25 +00:00
|
|
|
console.log(this.singleSupplierForm.value);
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-09-19 13:53:25 +00:00
|
|
|
this.api
|
2017-09-08 10:54:34 +00:00
|
|
|
.orgSupplier(this.singleSupplierForm.value)
|
2017-06-29 15:33:11 +00:00
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
console.log('data submitted!');
|
2017-09-19 13:53:25 +00:00
|
|
|
this.singleSupplierFormStatus = 'success';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.singleSupplierFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log( error._body );
|
2017-09-19 13:53:25 +00:00
|
|
|
this.singleSupplierFormStatus = 'send_failed';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.singleSupplierFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-06-29 15:33:11 +00:00
|
|
|
onSubmitEmployee() {
|
2017-09-19 13:53:25 +00:00
|
|
|
console.log(this.employeeForm.value);
|
2017-08-30 15:09:57 +00:00
|
|
|
|
2017-09-19 13:53:25 +00:00
|
|
|
this.api
|
2017-09-08 10:54:34 +00:00
|
|
|
.orgEmployee(this.employeeForm.value)
|
2017-06-29 15:33:11 +00:00
|
|
|
.subscribe(
|
|
|
|
result => {
|
|
|
|
console.log('data submitted!');
|
2017-09-19 13:53:25 +00:00
|
|
|
this.employeeFormStatus = 'success';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.employeeFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
},
|
|
|
|
error => {
|
|
|
|
console.log( error._body );
|
2017-09-19 13:53:25 +00:00
|
|
|
this.employeeFormStatus = 'send_failed';
|
2017-08-25 11:18:14 +00:00
|
|
|
console.log(this.employeeFormStatus);
|
2017-06-29 15:33:11 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-06-26 17:36:48 +00:00
|
|
|
|
|
|
|
}
|