Created
May 30, 2016 08:20
-
-
Save falendary/cb6e155fddb0b11ff141c0847cf164b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by szhitenev on 17.03.2016. | |
*/ | |
/// <reference path='../../../../../typings/tsd.d.ts' /> | |
import {Bill} from '../models/BillModel'; | |
export class ReportContentService { | |
private getCategories(items:Array<Bill>):any { | |
let i:number, x:number, c:number; | |
let categories:Array<any> = []; | |
let exist:boolean = false; | |
for (i = 0; i < items.length; i = i + 1) { | |
for (x = 0; x < items[i].fields.length; x = x + 1) { | |
if (!categories.length) { | |
categories.push({categoryName: items[i].fields[x].category.categoryName}) | |
} else { | |
exist = false; | |
for (c = 0; c < categories.length; c = c + 1) { | |
if (items[i].fields[x].category.categoryName === categories[c].categoryName) { | |
exist = true; | |
} | |
} | |
if (!exist) { | |
categories.push({categoryName: items[i].fields[x].category.categoryName}) | |
} | |
} | |
} | |
} | |
return categories | |
} | |
private getTotalQuantity(item:Bill):number { | |
let i:number; | |
let quantityTotal:number = 0; | |
for (i = 0; i < item.fields.length; i = i + 1) { | |
quantityTotal = quantityTotal + parseInt(item.fields[i].quantity.toString(), 10); | |
} | |
return quantityTotal; | |
} | |
public calcTotalPrice(items:Array<Bill>):number { | |
let sum:number = 0; | |
let i:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
sum = sum + items[i].total; | |
} | |
return sum; | |
} | |
public calcTotalQuantity(items:Array<Bill>):number { | |
let quantity:number = 0; | |
let i:number, x:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
for (x = 0; x < items[i].fields.length; x = x + 1) { | |
quantity = quantity + parseInt(items[i].fields[x].quantity.toString(), 10); | |
} | |
} | |
return quantity; | |
} | |
public getCategoriesInfo(items:Array<Bill>):any { | |
let categories:Array<any> = this.getCategories(items); | |
console.log('get Categories!', categories); | |
let i:number, f:number, c:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
for (f = 0; f < items[i].fields.length; f = f + 1) { | |
for (c = 0; c < categories.length; c = c + 1) { | |
if (items[i].fields[f].category.categoryName === categories[c].categoryName) { | |
if (!categories[c].quantityTotal) { | |
categories[c].quantityTotal = 0; | |
} | |
if (!categories[c].priceTotal) { | |
categories[c].priceTotal = 0; | |
} | |
categories[c].quantityTotal = categories[c].quantityTotal + parseInt(items[i].fields[f].quantity.toString(), 10); | |
categories[c].priceTotal = categories[c].priceTotal + parseInt(items[i].fields[f].total.toString(), 10); | |
} | |
} | |
} | |
} | |
return categories; | |
} | |
public getDearestProduct(items:Array<Bill>):any { | |
let dearestProduct:any = {productName: '', price: 0}; | |
let i:number, x:number; | |
let fieldPrice:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
for (x = 0; x < items[i].fields.length; x = x + 1) { | |
fieldPrice = parseInt(items[i].fields[x].price.toString(), 10); | |
if (dearestProduct.price < fieldPrice) { | |
dearestProduct.productName = items[i].fields[x].productName; | |
dearestProduct.price = fieldPrice; | |
} | |
} | |
} | |
return dearestProduct; | |
} | |
public getBiggestBill(items:Array<Bill>):any { | |
let biggestBill:any = {billsDate: null, quantityTotal: 0, priceTotal: 0}; | |
let i:number; | |
let itemTotal:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
itemTotal = this.getTotalQuantity(items[i]); | |
if (biggestBill.quantityTotal < itemTotal) { | |
biggestBill.priceTotal = items[i].total; | |
biggestBill.billsDate = moment(items[i].billsDate).format('DD.MM.YYYY'); | |
biggestBill.quantityTotal = itemTotal; | |
} | |
} | |
return biggestBill; | |
} | |
public getDearestBill(items:Array<Bill>):any { | |
let dearestBill:any = {billsDate: null, quantityTotal: 0, priceTotal: 0}; | |
let i:number; | |
let itemTotal:number; | |
for (i = 0; i < items.length; i = i + 1) { | |
itemTotal = this.getTotalQuantity(items[i]); | |
if (dearestBill.priceTotal < items[i].total) { | |
dearestBill.priceTotal = items[i].total; | |
dearestBill.billsDate = moment(items[i].billsDate).format('DD.MM.YYYY'); | |
dearestBill.quantityTotal = itemTotal; | |
} | |
} | |
return dearestBill; | |
} | |
public getBillsCount(items: Array<Bill>): number{ | |
return items.length; | |
} | |
public getMiddleBill(items:Array<Bill>):number { | |
let middlePrice:number; | |
let i:number; | |
let total:number = 0; | |
let quantity:number = 0; | |
for (i = 0; i < items.length; i = i + 1) { | |
total = total + items[i].total | |
} | |
middlePrice = total / items.length; | |
return parseInt(middlePrice.toFixed(0), 10); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by s.zhitenev on 17.11.2015. | |
* | |
*/ | |
/// <reference path="../../../../../typings/tsd.d.ts" /> | |
import {ZhNumKeyboardService} from '../services/ZhNumKeyboardService'; | |
interface ZhNumKeyboardDirectiveeScope extends ng.IScope { | |
field: number; | |
zhExternalCallback: Action; | |
} | |
import IAugmentedJQuery = angular.IAugmentedJQuery; | |
export class ZhNumKeyboardDirective { | |
private zhNumKeyboardBoxService: ZhNumKeyboardService = ZhNumKeyboardService.getInstance(); | |
public $compile: ng.ICompileService; | |
public $ionicScrollDelegate: any; | |
public cordovaKeyboard: any; | |
public restrict: string = 'AE'; | |
public replace: boolean = true; | |
public scope: Object = { | |
field: '=zhNumKeyboard', | |
}; | |
public link: ng.IDirectiveLinkFn = (scope: ZhNumKeyboardDirectiveeScope, elem: ng.IAugmentedJQuery, attrs: ng.IAttributes) => { | |
console.log('zhExternalCallback 1111', scope.zhExternalCallback); | |
let addedKeyboardBox: boolean = false; | |
let addDialog = () => { | |
addedKeyboardBox = true; | |
let directive: IAugmentedJQuery = this.$compile('<div ' | |
+ 'data-zh-num-keyboard-box' | |
+ '></div>')(scope.$parent); | |
$('body').append(directive); | |
}; | |
$(elem).on('focus',(event) =>{ | |
$(elem).addClass('focused'); | |
if(!$('.zh-num-keyboard').length && !addedKeyboardBox) { | |
console.log('len', $('.zh-num-keyboard').length); | |
addDialog(); | |
} | |
$('.zh-num-keyboard').removeClass('hideBox'); | |
$('.zh-num-keyboard').addClass('show'); | |
setTimeout(() => { | |
let scrollHeight = $('.scroll').height(); | |
let billsBodyHeight = $('.bill-body')[0].offsetTop + $('.bill-body').height(); | |
let screenWithKeyboardHeight = $('.scroll').height() - $('.zh-num-keyboard').height(); | |
if (billsBodyHeight > screenWithKeyboardHeight) { | |
$('.scroll').height(billsBodyHeight + $('.zh-num-keyboard').height() + $(elem).height() - 18); // H@H@ magic margin | |
this.$ionicScrollDelegate.scrollBottom(); | |
} | |
}, 0); | |
this.zhNumKeyboardBoxService.setFocus($(elem)[0]); | |
console.log('Numeric keyboard started', scope.field); | |
}); | |
}; | |
constructor($compile: ng.ICompileService, $ionicScrollDelegate: any) { | |
this.$compile = $compile; | |
this.$ionicScrollDelegate = $ionicScrollDelegate; | |
} | |
public static factory($compile: ng.ICompileService, $ionicScrollDelegate: any): ZhNumKeyboardDirective { | |
return new ZhNumKeyboardDirective($compile, $ionicScrollDelegate); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Bill} from "../models/BillModel"; | |
/** | |
* Created by sergey on 23.02.16. | |
*/ | |
'use strict'; | |
export class StatsRepository { | |
private getProductsBoughtTotal(bills: any): number { | |
let i: number, x: number; | |
let counter: number = 0; | |
for (i = 0; i < bills.length; i = i + 1) { | |
for (x = 0; x < bills[i].fields.length; x = x + 1) { | |
counter = counter + 1; | |
} | |
} | |
return counter; | |
}; | |
private getBillsTotal(bills: any): number { | |
let i: number; | |
let counter: number = 0; | |
for (i = 0; i < bills.length; i = i + 1) { | |
counter = counter + 1; | |
} | |
return counter; | |
}; | |
private getTotalSpent(bills: any): number { | |
let i: number; | |
let money: number = 0; | |
for (i = 0; i < bills.length; i = i + 1) { | |
money = money + bills[i].total; | |
} | |
return money; | |
}; | |
private getMostProduct(bills: any): number { | |
let max: number = 0; | |
let i: number, x: number; | |
let counter: number = 0; | |
for (i = 0; i < bills.length; i = i + 1) { | |
for (x = 0; x < bills[i].fields.length; x = x + 1) { | |
max = Math.max(max, bills[i].fields[x].price); | |
} | |
} | |
return max; | |
} | |
private getMostBill(bills: any): number { | |
let max: number = 0; | |
let i: number; | |
for (i = 0; i < bills.length; i = i + 1) { | |
max = Math.max(max, bills[i].total); | |
} | |
return max; | |
} | |
public getDaySpent(bills: any): number { | |
let i: number, x: number; | |
let getUsedDays = function (bills: any) { | |
let days: Array<any> = []; | |
var checkIfAlreadyAdded = function (days: any, day: any) { | |
let x: number; | |
let date: Date; | |
for (x = 0; x < days.length; x = x + 1) { | |
date = new Date(day); | |
if (new Date(days[x].dayDate).toString() === new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).toString()) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
let date: Date; | |
for (i = 0; i < bills.length; i = i + 1) { | |
date = new Date(bills[i].billsDate); | |
if (days.length) { | |
if (checkIfAlreadyAdded(days, bills[i].billsDate)) { | |
days.push({max: 0, dayDate: new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)}); | |
} | |
} else { | |
days.push({max: 0, dayDate: new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0)}); | |
} | |
} | |
return days; | |
}; | |
let findResult = function (days: any) { | |
let i: number; | |
let max: number = 0; | |
for (i = 0; i < days.length; i = i + 1) { | |
max = Math.max(max, days[i].max); | |
} | |
return max; | |
}; | |
let days: Array<any> = getUsedDays(bills); | |
console.log('14', days); | |
let date: Date; | |
for (i = 0; i < days.length; i = i + 1) { | |
for (x = 0; x < bills.length; x = x + 1) { | |
date = new Date(bills[x].billsDate); | |
if (new Date(days[i].dayDate).toString() === new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).toString()) { | |
days[i].max = days[i].max + 1; | |
} | |
} | |
} | |
return findResult(days); | |
} | |
public getStatsTotals(): Promise<any> { | |
return new Promise((resolve)=> { | |
window.kart.db.allDocs({ | |
include_docs: true, | |
startkey: 'bill', | |
endkey: 'bill\uffff' | |
}).then((response: any)=> { | |
let result: any = response.rows; | |
let total: any = {products: null, bills: null, spent: null}; | |
let most: any = {product: null, bill: null, daySpent: null}; | |
let bills: Array<Bill> = []; | |
let i: number; | |
for (i = 0; i < result.length; i = i + 1) { | |
bills.push(result[i].doc); | |
} | |
total.products = this.getProductsBoughtTotal(bills); | |
total.bills = this.getBillsTotal(bills); | |
total.spent = this.getTotalSpent(bills); | |
resolve(total); | |
}) | |
}) | |
} | |
public getStatsMost(): Promise<any> { | |
return new Promise((resolve)=> { | |
window.kart.db.allDocs({ | |
include_docs: true, | |
startkey: 'bill', | |
endkey: 'bill\uffff' | |
}).then((response: any)=> { | |
let result: any = response.rows; | |
let most: any = {product: null, bill: null, daySpent: null}; | |
let bills: Array<Bill> = []; | |
let i: number; | |
for (i = 0; i < result.length; i = i + 1) { | |
bills.push(result[i].doc); | |
} | |
console.log('bills', bills); | |
most.product = this.getMostProduct(bills); | |
most.bill = this.getMostBill(bills); | |
most.daySpent = this.getDaySpent(bills); | |
resolve(most); | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment