diff --git a/src/container/SalesContainer/index.js b/src/container/SalesContainer/index.js index cf08b2e..b207aab 100644 --- a/src/container/SalesContainer/index.js +++ b/src/container/SalesContainer/index.js @@ -542,15 +542,18 @@ export default class SalesContainer extends React.Component { quantity.commission_amount ) { this.props.attendantStore.find(quantity.attendantName).then(result => { - let discountValue = - parseFloat(quantity.discount) > 0 - ? price * qty - parseFloat(quantity.discount) / 100 * (price * qty) - : price * qty; - line.setCommissionAttendantName(quantity.attendantName); - line.setCommissionRate(result.commission, 10); - line.setCommissionAmount( - parseFloat(result.commission, 10) / 100 * discountValue, - ); + if (result) { + let discountValue = + parseFloat(quantity.discount) > 0 + ? price * qty - + parseFloat(quantity.discount) / 100 * (price * qty) + : price * qty; + line.setCommissionAttendantName(quantity.attendantName); + line.setCommissionRate(result.commission, 10); + line.setCommissionAmount( + parseFloat(result.commission, 10) / 100 * discountValue, + ); + } }); } // unselect the line diff --git a/src/container/SettingsContainer/index.js b/src/container/SettingsContainer/index.js index 7af9e5d..6b985e3 100644 --- a/src/container/SettingsContainer/index.js +++ b/src/container/SettingsContainer/index.js @@ -5,10 +5,10 @@ import BluetoothSerial from "react-native-bluetooth-serial"; import { observer, inject } from "mobx-react/native"; import { NavigationActions } from "react-navigation"; import { BluetoothStatus } from "react-native-bluetooth-status"; -// import { syncObjectValues } from "../../store/PosStore/syncInBackground"; +import { syncObjectValues } from "../../store/PosStore/syncInBackground"; import Settings from "@screens/Settings"; -import { syncData } from "./sync"; +// import { syncData } from "./sync"; @inject( "printerStore", @@ -605,31 +605,35 @@ export default class SettingsContainer extends React.Component { } syncAll(status) { - const { url, user_name, password } = this.props.printerStore.sync[0]; + // console.log("SYYYYYYNC") + // const { url, user_name, password } = this.props.printerStore.sync[0]; + // + // syncData(url, { + // username: user_name, + // password: password, + // }) + // .then(res => { + // if (res.uptodate) { + // Toast.show({ + // text: "Already up-to-date", + // buttonText: "Okay", + // }); + // } else { + // if (res.items) { + // this.props.itemStore.addBulk(res.items); + // } + // } + // }) + // .catch(err => { + // Toast.show({ + // type: "danger", + // text: err.toString(), + // buttonText: "Okay", + // }); + // }); - syncData(url, { - username: user_name, - password: password, - }) - .then(res => { - if (res.uptodate) { - Toast.show({ - text: "Already up-to-date", - buttonText: "Okay", - }); - } else { - if (res.items) { - this.props.itemStore.addBulk(res.items); - } - } - }) - .catch(err => { - Toast.show({ - type: "danger", - text: err.toString(), - buttonText: "Okay", - }); - }); + const storeProps = this.props; + syncObjectValues(status, storeProps, false); } onSyncSave() { diff --git a/src/container/SettingsContainer/sync.js b/src/container/SettingsContainer/sync.js index c9d02fc..f092639 100644 --- a/src/container/SettingsContainer/sync.js +++ b/src/container/SettingsContainer/sync.js @@ -1,10 +1,26 @@ import Frappe from "react-native-frappe-fetch"; +var validUrl = require("valid-url"); +import { Toast } from "native-base"; module.exports.syncData = function(url, credentials) { const { username, password } = credentials; - return Frappe.createClient({ url, username, password }) - .then(res => Frappe.Client.postApi("tailpos_sync.sync")) - .then(res => res.json()) - .then(res => res.message); + if (validUrl.isWebUri(url.toLowerCase())) { + return Frappe.createClient({ url, username, password }) + .then(res => { + Frappe.Client.postApi("tailpos_sync.sync"); + }) + .then(res => { + res.json(); + }) + .then(res => { + res.message; + }); + } else { + Toast.show({ + text: "Invalid URL", + type: "danger", + duration: 5000, + }); + } }; diff --git a/src/store/PosStore/AttendantStore.js b/src/store/PosStore/AttendantStore.js index 44171d7..fca29a3 100644 --- a/src/store/PosStore/AttendantStore.js +++ b/src/store/PosStore/AttendantStore.js @@ -90,11 +90,31 @@ const AttendantStore = types if (obj) { return obj; } else { - await db.get(id).then(doc => { - return Attendant.create(JSON.parse(JSON.stringify(doc))); - }); + // await db.get(id).then(doc => { + // if(doc){ + // return Attendant.create(JSON.parse(JSON.stringify(doc))); + // + // } else { + // return null; + // } + // }); + db + .find({ + selector: { + _id: { $regex: `.*${id}.*` }, + }, + }) + .then(result => { + const { docs } = result; + if (docs.length > 0) { + return Attendant.create( + JSON.parse(JSON.stringify(result.docs[0])), + ); + } else { + return null; + } + }); } - return null; }, getData() { return new Promise(function(resolve, reject) { diff --git a/src/store/PosStore/CategoryStore.js b/src/store/PosStore/CategoryStore.js index 14b6418..d8da8d3 100644 --- a/src/store/PosStore/CategoryStore.js +++ b/src/store/PosStore/CategoryStore.js @@ -86,16 +86,40 @@ const Store = types if (!obj && id !== "No Category") { // If category is found in the databaseeeeee db - .get(id) - .then(doc => - resolve(Category.create(JSON.parse(JSON.stringify(doc)))), - ); + .find({ + selector: { + _id: { $regex: `.*${id}.*` }, + }, + }) + .then(result => { + const { docs } = result; + if (docs.length > 0) { + resolve( + Category.create(JSON.parse(JSON.stringify(result.docs[0]))), + ); + } else { + resolve(null); + } + }); + // db + // .get(id) + // .then(doc =>{ + // console.log(doc) + // if(doc){ + // console.log("TRUUUE") + // } else { + // console.log("NULL") + // resolve(null); + // } + // }).catch(e => { + // throw e + // }) + // resolve(null); } else if (obj) { resolve(obj); // if found object } // If no object found - resolve(null); }); }, setCategory(category) { diff --git a/src/store/PosStore/CustomerStore.js b/src/store/PosStore/CustomerStore.js index 7e1d30b..5428ace 100644 --- a/src/store/PosStore/CustomerStore.js +++ b/src/store/PosStore/CustomerStore.js @@ -68,11 +68,30 @@ const Store = types if (obj) { return obj; } else { - await db.get(id).then(doc => { - return Customer.create(JSON.parse(JSON.stringify(doc))); - }); + db + .find({ + selector: { + _id: { $regex: `.*${id}.*` }, + }, + }) + .then(result => { + const { docs } = result; + if (docs.length > 0) { + return Customer.create( + JSON.parse(JSON.stringify(result.docs[0])), + ); + } else { + return null; + } + }); + // await db.get(id).then(doc => { + // if(doc){ + // return Customer.create(JSON.parse(JSON.stringify(doc))); + // } else { + // return null; + // } + // }); } - return null; }, delete(data) { destroy(data); diff --git a/src/store/PosStore/DiscountStore.js b/src/store/PosStore/DiscountStore.js index 73a8860..c6b5379 100644 --- a/src/store/PosStore/DiscountStore.js +++ b/src/store/PosStore/DiscountStore.js @@ -91,12 +91,23 @@ const Store = types if (obj) { return obj; } else { - db.get(id).then(doc => { - return Discount.create(JSON.parse(JSON.stringify(doc))); - }); + db + .find({ + selector: { + _id: { $regex: `.*${id}.*` }, + }, + }) + .then(result => { + const { docs } = result; + if (docs.length > 0) { + return Discount.create( + JSON.parse(JSON.stringify(result.docs[0])), + ); + } else { + return null; + } + }); } - - return null; }, findFromRows(id) { for (var i = 0; i < self.rows.length; i++) { diff --git a/src/store/PosStore/ItemStore.js b/src/store/PosStore/ItemStore.js index bf6effd..00cc417 100644 --- a/src/store/PosStore/ItemStore.js +++ b/src/store/PosStore/ItemStore.js @@ -151,15 +151,22 @@ const Store = types updateLengthObjects(obj) { if (obj) { let objectLength = JSON.parse(self.categoryLengths); - + var length = false; for (let i = 0; i < objectLength.length; i += 1) { if (obj === objectLength[i].categoryId) { objectLength[i].categoryId = obj; objectLength[i].categoryLength += 1; + length = true; } } - - self.categoryLengths = JSON.stringify(objectLength); + if (!length) { + self.addCategoryLength({ + categoryId: obj, + categoryLength: 1, + }); + } else { + self.categoryLengths = JSON.stringify(objectLength); + } } }, updateLengthObjectsDelete(obj) { @@ -186,11 +193,28 @@ const Store = types if (obj) { return obj; } else { - db.get(id).then(doc => { - return Item.create(JSON.parse(JSON.stringify(doc))); - }); + // db.get(id).then(doc => { + // if(doc){ + // return Item.create(JSON.parse(JSON.stringify(doc))); + // } else { + // return null; + // } + // }); + db + .find({ + selector: { + _id: { $regex: `.*${id}.*` }, + }, + }) + .then(result => { + const { docs } = result; + if (docs.length > 0) { + return Item.create(JSON.parse(JSON.stringify(result.docs[0]))); + } else { + return null; + } + }); } - return null; }, findName(name, price) { return new Promise(function(resolve, reject) { diff --git a/src/store/PosStore/syncInBackground.js b/src/store/PosStore/syncInBackground.js index 8cf9943..3b5ce9d 100644 --- a/src/store/PosStore/syncInBackground.js +++ b/src/store/PosStore/syncInBackground.js @@ -19,22 +19,22 @@ export function syncObjectValues(status, store, jobStatus) { .then(async resultFromErpnext => { if (resultFromErpnext) { for (let x = 0; x < resultFromErpnext.data.length; x += 1) { - if (resultFromErpnext.data[x].tableNames === "Item") { - await itemSync(resultFromErpnext.data[x], store); - } else if ( - resultFromErpnext.data[x].tableNames === "Categories" - ) { + if (resultFromErpnext.data[x].tableNames === "Categories") { await categorySync(resultFromErpnext.data[x], store); + } else if (resultFromErpnext.data[x].tableNames === "Customer") { + await customerSync(resultFromErpnext.data[x], store); } else if (resultFromErpnext.data[x].tableNames === "Discounts") { await discountSync(resultFromErpnext.data[x], store); } else if ( resultFromErpnext.data[x].tableNames === "Attendants" ) { await attendantSync(resultFromErpnext.data[x], store); - } else if (resultFromErpnext.data[x].tableNames === "Customer") { - await customerSync(resultFromErpnext.data[x], store); } + // else if (resultFromErpnext.data[x].tableNames === "Item") { + // await itemSync(resultFromErpnext.data[x], store); + // } } + if (resultFromErpnext.deleted_documents.length > 0) { for ( let x = 0; @@ -48,6 +48,12 @@ export function syncObjectValues(status, store, jobStatus) { } } } + + for (let xx = 0; xx < resultFromErpnext.data.length; xx += 1) { + if (resultFromErpnext.data[xx].tableNames === "Item") { + await itemSync(resultFromErpnext.data[xx], store); + } + } await changeSyncStatusValue(result, store); if (!jobStatus) { Toast.show({ @@ -76,13 +82,14 @@ export async function itemSync(itemObject, store) { let categoryIds = await store.categoryStore.searchLengthName( itemObject.syncObject.category, ); - - // if(categoryIds) { categoryId = categoryIds._id; + store.itemStore.updateLengthObjects(categoryIds._id); + // if(categoryIds) { // } } else { categoryId = "No Category"; } + if (itemObjectResult) { itemObjectResult.edit({ _id: itemObject.syncObject.id, @@ -172,10 +179,7 @@ export async function itemSync(itemObject, store) { itemObject.syncObject.favorite !== null ? itemObject.syncObject.favorite : "", - category: - itemObject.syncObject.category !== null - ? itemObject.syncObject.category - : "", + category: categoryId, taxes: "[]", dateUpdated: Date.now(), syncStatus: true, @@ -193,7 +197,7 @@ export async function categorySync(categoryObject, store) { await store.categoryStore .find(categoryObject.syncObject.id) .then(categoryObjectResult => { - if (categoryObjectResult) { + if (categoryObjectResult !== null) { categoryObjectResult.edit({ _id: categoryObject.syncObject.id, name: @@ -247,7 +251,6 @@ export async function discountSync(discountObject, store) { let discountObjectResult = await store.discountStore.find( discountObject.syncObject.id, ); - if (discountObjectResult) { discountObjectResult.edit({ _id: