Last active
November 22, 2019 18:00
-
-
Save acestudiooleg/e8366bb0c7d3752cd409e5227d257b95 to your computer and use it in GitHub Desktop.
google scripts taxi calculation
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
| var UKLON_TAX = 12; | |
| var UBER_TAX = 7; | |
| var ON_TAXI_TAX = 9; | |
| var CARD_TAX = 2.5; | |
| var FUEL_COST = 31; | |
| var FUEL_AVG = 10.5; | |
| var t = '\ | |
| body{margin:0}\ | |
| h1, h3 {\ | |
| text-align: center;\ | |
| color: #4C596C;\ | |
| }\ | |
| h1 {\ | |
| font-size: 40px;\ | |
| color: #4C596C;\ | |
| }\ | |
| table {\ | |
| font-size: 14px;\ | |
| background: white;\ | |
| width: 100%;\ | |
| border-collapse: collapse;\ | |
| text-align: left;\ | |
| margin: 0 auto 20px;\ | |
| }\ | |
| td {\ | |
| transition: .3s linear;\ | |
| }\ | |
| .table1 th {\ | |
| font-size: 14px;\ | |
| font-weight: normal;\ | |
| color: #039;\ | |
| border-bottom: 2px solid #6678b1;\ | |
| padding: 5px 4px;\ | |
| }\ | |
| .table1 td {\ | |
| color: #669;\ | |
| padding: 4px 4px;\ | |
| }\ | |
| .table1 tr:hover td{\ | |
| color: #6699ff;\ | |
| }\ | |
| .bigButton {\ | |
| padding: 15px;\ | |
| width: 100%;\ | |
| }\ | |
| .bigButton a{\ | |
| font-size: 42px;\ | |
| }\ | |
| .buttonsContainer {\ | |
| display: flex;\ | |
| width: 100%;\ | |
| justify-content: space-around;\ | |
| }\ | |
| .container {\ | |
| display: flex;\ | |
| flex-direction: row;\ | |
| width: 700px;\ | |
| flex-wrap: wrap;\ | |
| }\ | |
| \ | |
| .item {\ | |
| display: flex;\ | |
| width: 150px;\ | |
| }\ | |
| .calendar {\ | |
| display: flex;\ | |
| flex-direction: row;\ | |
| width: 700px;\ | |
| flex-wrap: wrap;\ | |
| }\ | |
| '; | |
| var url = 'https://script.google.com/macros/s/AKfycby1HRurCl4DyxM3FvWwOqAu3QiEz-qNCEBrceTUWrKm/dev'; | |
| var mmm = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | |
| var monthesArr = []; | |
| for(var i = 0; i < 12; i++) { | |
| monthesArr[i] = ['<div class="item"><button class="bigButton"><a target="_blank" href="',url,'?m=',i,'">', mmm[i],'</a></button></div>'].join(''); | |
| } | |
| var buttonAll = ['<div class="item"><button class="bigButton"><a target="_blank" href="',url,'?all=1">ALL</a></button></div>'].join(''); | |
| var monthes = ['<div class="container">', monthesArr.join(''), buttonAll,'</div>'].join(''); | |
| function toHtmlBody(content) { | |
| return [ | |
| '<style>', t,'</style>', | |
| '<div>', | |
| content, | |
| '</div>' | |
| ].join(''); | |
| }; | |
| var calendar = []; | |
| for(var ii=1; ii<=31; ii++) { | |
| calendar.push('<div class="item"><button class="bigButton"><a target="_blank" href="'+ url +'?d='+ ii + '">'+ ii + '</a></button></div>'); | |
| } | |
| var calendarContainer = '<div class="calendar">' + calendar.join('') +'</div>'; | |
| var buttonsContainer = '<div class="buttonsContainer">' + monthes + calendarContainer+ '</div>'; | |
| function doGet(e) { | |
| var month = e && e.parameter && e.parameter.m; | |
| var dayParam = e && e.parameter && e.parameter.d || new Date().getDate(); | |
| var all = e && e.parameter && e.parameter.all; | |
| var day = new Date(); | |
| day.setDate(dayParam); | |
| if (!month) { | |
| month = new Date().getMonth(); | |
| } | |
| var values = getValues().filter(function(el){ return el.some(Boolean)}); | |
| var filteredValues = all ? values : values.filter(filterByMonth(Number(month))); | |
| var x = calcValues(filteredValues, day); | |
| var tableHtml = buildTable([['N', 'Service', 'Pay Type', 'Distance', 'Cost', 'Profit', 'Balance Type', 'Amount']].concat(x.table)); | |
| var table2 = buildTable([ | |
| ['Distance', 'Money', 'Uber', 'Uklon', 'Cash', 'Card'], | |
| [x.km+' KM', x.money +' UAH', x.uber+'%', x.uklon+'%', x.cash+'%', x.card+'%'] | |
| ]); | |
| var h = [ | |
| '<h1>Profit: ', x.total.toFixed(2), ' UAH (', x.percentProfit,'%)</h1>', | |
| '<h1>Balance: ', x.balance.toFixed(2), ' UAH (-', 100 - x.percentProfit,'%)</h1>', | |
| '<h1>Car Cost: ', x.moneyAmount, ' UAH</h1>', | |
| '<h1>Today: ', x.todayProfit, ' UAH (', day.toDateString(), ')</h1>', | |
| table2, | |
| tableHtml, | |
| buttonsContainer | |
| ].join(''); | |
| return HtmlService.createHtmlOutput(toHtmlBody(h)); | |
| } | |
| function getTaxiDoc() { | |
| return SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1n5KZ9F-A-JBKHujzgN8aBCiTxjSa4yCuxi0-5MVX4x4/edit#gid=1021429125"); | |
| } | |
| function getValues() { | |
| const taxiDoc = getTaxiDoc(); | |
| const operations = taxiDoc.getSheets()[0]; | |
| const range = operations.getRange(2, 1, 1000, 9); | |
| return range.getValues(); | |
| } | |
| function calcProfit(service, type, path, cost){ | |
| const isUklon = service === 'Uklon'; | |
| const isUber = service === 'Uber'; | |
| const isOnTaxi = service === 'OnTaxi'; | |
| const isCard = type === 'Card'; | |
| const fuelCost = FUEL_AVG / 100 * FUEL_COST * path; | |
| const cardTax = (cost/100*CARD_TAX); | |
| const uklonTax = (cost/100*UKLON_TAX); | |
| const uberTax = (cost/100*UBER_TAX); | |
| const onTaxiTax = (cost/100*ON_TAXI_TAX); | |
| var tax = 0; | |
| if (isUklon) { | |
| tax = isCard ? uklonTax + cardTax : uklonTax | |
| } | |
| if (isUber) { | |
| tax = uberTax | |
| } | |
| if (onTaxiTax) { | |
| tax = onTaxiTax | |
| } | |
| return Number((cost - fuelCost - tax).toFixed(2)); | |
| } | |
| function buildTable(arr){ | |
| const rows = arr.map(function(row){ | |
| const cols = row.map(function(col){ | |
| return ['<td>',col,'</td>'].join(''); | |
| }).join(''); | |
| return ['<tr>', cols, '</tr>'].join(''); | |
| }).join(''); | |
| return ['<table class="table1" cellpadding="1" cellspacing="1">', rows, '</table>'].join(''); | |
| } | |
| function percent(val, len){ | |
| return (val * 100 / len).toFixed(2); | |
| } | |
| function filterByMonth(currentMonth) { | |
| return function(row){ | |
| const time = row[0]; | |
| const month = time.getMonth(); | |
| return month === currentMonth; | |
| } | |
| } | |
| function calcValues(values, day){ | |
| var total = 0; | |
| var km = 0; | |
| var money = 0; | |
| var uber = 0; | |
| var onTaxi = 0; | |
| var uklon = 0; | |
| var cash = 0; | |
| var card = 0; | |
| var moneyAmount = 0; | |
| var serviceLen = 0; | |
| var paytypeLen = 0; | |
| var todayProfit = 0; | |
| const arr = values.map(function(row, i) { | |
| const service = row[1]; | |
| const type = row[2]; | |
| const path = Number(row[3]); | |
| const cost = Number(row[4]); | |
| const typeOfMinus = row[6]; | |
| const moneyAmnt = Number(row[7]); | |
| const time = new Date(row[8]); | |
| const profit = calcProfit(service, type, path, cost); | |
| const isToday = time.toDateString() === day.toDateString(); | |
| todayProfit += isToday ? profit : 0 | |
| total += profit; | |
| km += path; | |
| money += cost; | |
| uber += service === 'Uber' ? 1 : 0; | |
| uklon += service === 'Uklon' ? 1 : 0; | |
| onTaxi += service === 'OnTaxi' ? 1 : 0; | |
| serviceLen += service ? 1 : 0; | |
| paytypeLen += type ? 1 : 0; | |
| cash += type === 'Cash' ? 1 : 0; | |
| card += type === 'Card' ? 1 : 0; | |
| moneyAmount -= moneyAmnt; | |
| return [i+1, service, type, path, cost + ' UAH', profit + ' UAH', typeOfMinus, moneyAmnt]; | |
| }); | |
| var len = values.length; | |
| return { | |
| total: total, | |
| km: km, | |
| money:money, | |
| moneyAmount: moneyAmount * -1, | |
| balance: moneyAmount + total, | |
| percentProfit: percent(total, moneyAmount) * -1, | |
| table: arr, | |
| todayProfit: todayProfit, | |
| uber: percent(uber, serviceLen), | |
| onTaxi: percent(onTaxi, serviceLen), | |
| uklon: percent(uklon, serviceLen), | |
| card: percent(card, paytypeLen), | |
| cash: percent(cash, paytypeLen) | |
| }; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment