Skip to content

Commit

Permalink
POS: reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Feb 3, 2023
1 parent fbf7f66 commit 56e1a81
Show file tree
Hide file tree
Showing 9 changed files with 947 additions and 244 deletions.
8 changes: 8 additions & 0 deletions Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import Olympians from './views/Settings/Olympians';
import Gods from './views/Settings/Gods';
import Mortals from './views/Settings/Mortals';
import PointOfSale from './views/Settings/PointOfSale';
import PointOfSaleRecon from './views/Settings/PointOfSaleRecon';
import PointOfSaleReconExport from './views/Settings/PointOfSaleReconExport';
import PaymentsSettings from './views/Settings/PaymentsSettings';

// Routing
Expand Down Expand Up @@ -251,6 +253,12 @@ const AppScenes = {
PointOfSaleSettings: {
screen: PointOfSale
},
PointOfSaleRecon: {
screen: PointOfSaleRecon
},
PointOfSaleReconExport: {
screen: PointOfSaleReconExport
},
PaymentsSettings: {
screen: PaymentsSettings
}
Expand Down
3 changes: 3 additions & 0 deletions assets/images/SVG/Export.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@
"views.Settings.POS.confPref": "Confirmation preference",
"views.Settings.POS.disableTips": "Disable tips",
"views.Settings.POS.devMode": "Developer mode",
"views.Settings.POS.recon": "Reconciliation",
"views.Settings.POS.reconExport": "Reconciliation Export",
"views.Transaction.title": "Transaction",
"views.Transaction.totalFees": "Total Fees",
"views.Transaction.transactionHash": "Transaction Hash",
Expand Down Expand Up @@ -546,6 +548,7 @@
"pos.views.Order.tipBitcoin": "Tip (Bitcoin)",
"pos.views.Order.totalFiat": "Total (fiat)",
"pos.views.Order.totalBitcoin": "Total (Bitcoin)",
"pos.views.Order.total": "Total",
"pos.views.Order.paymentType": "Payment type",
"pos.views.Settings.PointOfSale.authWarning": "Warning: no password or PIN set",
"pos.views.Settings.PointOfSale.backendWarning": "Warning: currently only LND nodes are able to mark orders as paid",
Expand Down
14 changes: 14 additions & 0 deletions models/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ export default class Order extends BaseModel {
return itemsList;
}

@computed public get autoGratuity(): string {
let autoGratuity = '';
const itemCount = this.getItemCount;
for (let i = 0; i < itemCount; i++) {
const line_item: any = this.line_items[i];
if (line_item.name.toLowerCase().includes('gratuity')) {
autoGratuity = Number(
line_item.base_price_money.amount / 100
).toFixed(2);
}
}
return autoGratuity;
}

@computed public get getTotalMoney(): string {
return Number(this.total_money.amount / 100).toFixed(2);
}
Expand Down
138 changes: 138 additions & 0 deletions stores/PosStore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { action, observable } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';
import ReactNativeBlobUtil from 'react-native-blob-util';
import BigNumber from 'bignumber.js';

import { SATS_PER_BTC } from './UnitsStore';
import SettingsStore from './SettingsStore';
import Order from '../models/Order';

Expand All @@ -20,6 +22,13 @@ export default class PosStore {
@observable public paidOrders: Array<Order> = [];
@observable public filteredOpenOrders: Array<Order> = [];
@observable public filteredPaidOrders: Array<Order> = [];
// recon
@observable public completedOrders: Array<Order> = [];
@observable public reconTotal: string = '0.00';
@observable public reconTax: string = '0.00';
@observable public reconTips: string = '0.00';
@observable public reconExport: string = '';
//
@observable public loading = false;
@observable public error = false;

Expand Down Expand Up @@ -149,6 +158,135 @@ export default class PosStore {
});
};

@action
public getOrdersHistorical = async (hours = 24) => {
const { squareAccessToken, squareLocationId, squareDevMode } =
this.settingsStore.settings.pos;
this.loading = true;
this.error = false;
const apiHost = squareDevMode
? 'https://connect.squareupsandbox.com'
: 'https://connect.squareup.com';
ReactNativeBlobUtil.fetch(
'POST',
`${apiHost}/v2/orders/search`,
{
Authorization: `Bearer ${squareAccessToken}`,
'Content-Type': 'application/json'
},
JSON.stringify({
limit: 10000,
location_ids: [squareLocationId],
query: {
filter: {
date_time_filter: {
created_at: {
start_at: new Date(
Date.now() - hours * 60 * 60 * 1000
).toISOString(),
end_at: new Date().toISOString()
}
},
state_filter: {
states: ['COMPLETED']
}
}
},
sort: {
sort_field: 'CREATED_AT',
sort_order: 'DESC'
}
})
)
.then(async (response: any) => {
const status = response.info().status;
if (status == 200) {
this.loading = false;
let orders = response
.json()
.orders.map((order: any) => new Order(order))
.filter((order: any) => {
return (
order.tenders &&
order.tenders[0] &&
order.tenders[0].note &&
(order.tenders[0].note
.toLowerCase()
.includes('zeus') ||
order.tenders[0].note
.toLowerCase()
.includes('zues') ||
order.tenders[0].note
.toLowerCase()
.includes('bitcoin') ||
order.tenders[0].note
.toLowerCase()
.includes('btc'))
);
});

let total = 0;
let tax = 0;
let tips = 0;
let exportString =
'orderId, totalSats, tipSats, rateFull, rateNumerical, type, tx\n';
const enrichedOrders = await Promise.all(
orders.map(async (order: any) => {
const payment = await EncryptedStorage.getItem(
`pos-${order.id}`
);
let tip;
if (payment) {
order.payment = JSON.parse(payment);
const {
orderId,
orderTotal,
orderTip,
exchangeRate,
rate,
type,
tx
} = order.payment;
tip = new BigNumber(orderTip)
.multipliedBy(rate)
.dividedBy(SATS_PER_BTC)
.toFixed(2);

exportString += `${orderId}, ${orderTotal}, ${orderTip}, ${exchangeRate}, ${rate}, ${type}, ${tx}\n`;
}

// tally totals
total +=
Number(order.getTotalMoney) +
Number(order.getTaxMoney);
tax += Number(order.getTaxMoney);
tips += tip
? Number(tip)
: order.autoGratuity
? Number(order.autoGratuity)
: 0;
return order;
})
);

this.completedOrders = enrichedOrders;
this.reconTotal = total.toFixed(2);
this.reconTax = tax.toFixed(2);
this.reconTips = tips.toFixed(2);
this.reconExport = exportString;
} else {
this.completedOrders = [];
this.loading = false;
this.error = true;
}
})
.catch((err) => {
console.error('POS get historical orders err', err);
this.completedOrders = [];
this.loading = false;
});
};

resetOrders = () => {
this.openOrders = [];
this.paidOrders = [];
Expand Down
Loading

0 comments on commit 56e1a81

Please sign in to comment.