public function removeItem($result) { // validate json if (!isset($this->data->id) || !isset($this->data->itemid)) { $result['error'] = "Sale & item id must be provided"; return $result; } // update item record $itemMdl = new SaleItemsModel(); if ($itemMdl->removeById($this->data->itemid) === false) { $result['error'] = "Could not remove item record: " . $itemMdl->errorInfo; return $result; } // delete item in json foreach ($this->invoice->items as $key => $item) { if ($this->data->itemid == $item->id) { $this->data->sitemid = $item->sitemid; $this->data->qty = $item->qty; unset($this->invoice->items[$key]); $this->invoice->items = array_values($this->invoice->items); break; } } // Update invoice totals $this->calculateInvoice(); // update invoice data if ($this->saveInvoiceData() === false) { $result['error'] = "Could not commit invoice data: " . $this->invMdl->errorInfo; return $result; } else { // increment stock if ($this->data->sitemid > 0) { $wposStock = new WposAdminStock(); $wposStock->incrementStockLevel($this->data->sitemid, 0, $this->data->qty, false); } // Create transaction history record WposTransactions::addTransactionHistory($this->id, $_SESSION['userId'], "Modified", "Item Removed"); // log data Logger::write("Invoice item removed for invoice id: " . $this->id, "INVOICE", json_encode($this->data)); } $result['data'] = $this->invoice; return $result; }
/** * Retract a void or refund using the sale id and void/refund processdt * @param $result * @return mixed */ public function removeVoidRecord($result) { $jsonval = new JsonValidate($this->data, '{"id":1, "processdt":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } // find entry and delete $salesMdl = new SalesModel(); $voidMdl = new SaleVoidsModel(); $refitems = []; // retrive the sales record if (($sale = $salesMdl->getById($this->data->id)) !== false) { // Decode JSON and remove the refund/void $jsondata = json_decode($sale[0]['data']); $recfound = false; $foundrecord = null; $foundtype = null; // check if the void record is a match if ($jsondata->voiddata->processdt == $this->data->processdt) { $foundrecord = $jsondata->voiddata; unset($jsondata->voiddata); $recfound = true; $foundtype = 'void'; } else { // no void record found with that timestamp, try refunds if ($jsondata->refunddata != null) { foreach ($jsondata->refunddata as $key => $refund) { if ($refund->processdt == $this->data->processdt) { // add the items to the array so we can remove them from qty refunded $refitems = $jsondata->refunddata[$key]->items; // unset the array value, this outputs objects so we need to reformat as array $foundrecord = $jsondata->refunddata[$key]; unset($jsondata->refunddata[$key]); $jsondata->refunddata = array_values($jsondata->refunddata); if (sizeof($jsondata->refunddata) == 0) { unset($jsondata->refunddata); } $recfound = true; $foundtype = 'refund'; break; } } } } // calculate updated status $status = isset($jsondata->voiddata) ? 3 : (isset($jsondata->refunddata) ? 2 : 1); if ($recfound) { // remove the void db record if ($voidMdl->removeBySale($this->data->id, $this->data->processdt)) { if (sizeof($refitems) > 0) { // if its a refund, remove qty refunded $saleItemsMdl = new SaleItemsModel(); // Decrement refunded quantities in the sale_items table foreach ($refitems as $item) { $saleItemsMdl->incrementQtyRefunded($this->data->id, $item->id, $item->numreturned, false); } } if (!$salesMdl->edit($this->data->id, null, json_encode($jsondata), $status)) { $result["error"] = "Could not update sales table. Error:" . $salesMdl->errorInfo; } else { $result['data'] = $jsondata; // if sale has been unvoided, remove item stock from the location where created if ($foundtype == 'void' && $status != 3 && sizeof($jsondata->items) > 0) { $wposStock = new WposAdminStock(); foreach ($jsondata->items as $item) { if ($item->sitemid > 0) { $wposStock->incrementStockLevel($item->sitemid, $jsondata->locid, $item->qty, true); } } } // Create transaction history record WposTransactions::addTransactionHistory($this->data->id, $_SESSION['userId'], "Retract", "Transaction Void/Refund Retracted"); // Success; log data Logger::write("Retracted void/refund from:" . $jsondata->ref, "RETRACT", json_encode($foundrecord)); } } else { $result["error"] = "Could not remove void record. Error:" . $voidMdl->errorInfo; } } else { $result["error"] = "Could not find the record in the JSON data: " . print_r($jsondata); } } else { $result["error"] = "Could not fetch the sales record. Error:" . $salesMdl->errorInfo; } return $result; }
/** * Insert transaction item records * @return bool */ private function insertTransactionItems() { $itemsMdl = new SaleItemsModel(); //$stockMdl = new StockModel(); $wposStock = new WposAdminStock(); foreach ($this->jsonobj->items as $key => $item) { if (!($res = $itemsMdl->create($this->id, $item->sitemid, $item->ref, $item->qty, $item->name, $item->desc, $item->taxid, $item->tax, $item->unit, $item->price))) { $this->itemErr = $itemsMdl->errorInfo; return false; } // decrement stock level if ($item->sitemid > 0) { /*$stockMdl->incrementStockLevel($item->sitemid, $this->jsonobj->locid, $item->qty, true);*/ $wposStock->incrementStockLevel($item->sitemid, $this->jsonobj->locid, $item->qty, true); } $this->jsonobj->items[$key]->id = $res; } return true; }
/** * routes api calls and returns the result, allows for multiple API calls at once * @param $action * @param $data * @param $result * @return array|mixed */ function routeApiCall($action, $data, $result) { global $auth; $notinprev = false; // Check for action in unprotected area (does not require permission) switch ($action) { // POS Specific case "config/get": $setup = new WposPosSetup($data); $result = $setup->getDeviceRecord($result); break; case "items/get": $jsondata = new WposPosData(); $result = $jsondata->getItems($result); break; case "sales/get": $jsondata = new WposPosData($data); $result = $jsondata->getSales($result); break; case "tax/get": $jsondata = new WposPosData(); $result = $jsondata->getTaxes($result); break; case "customers/get": $jsondata = new WposPosData(); $result = $jsondata->getCustomers($result); break; case "devices/get": $jsondata = new WposPosData(); $result = $jsondata->getPosDevices($result); break; case "locations/get": $jsondata = new WposPosData(); $result = $jsondata->getPosLocations($result); break; case "orders/set": $sale = new WposPosSale($data); $result = $sale->setOrder($result); break; case "orders/remove": $sale = new WposPosSale($data); $result = $sale->removeOrder($result); break; case "sales/add": $sale = new WposPosSale($data); $result = $sale->insertTransaction($result); break; case "sales/void": // also used for sale refunds $sale = new WposPosSale($data, false); $result = $sale->insertVoid($result); break; case "sales/search": $sale = new WposPosData(); if (isset($data)) { $result = $sale->searchSales($data, $result); } break; case "sales/updatenotes": $sale = new WposPosSale($data, false); $result = $sale->updateTransationNotes($result); break; case "transactions/get": $trans = new WposTransactions($data); $result = $trans->getTransaction($result); break; default: $notinprev = true; } if ($notinprev == false) { // an action has been executed: return the data return $result; } // Check if user is allowed to use this API request if ($auth->isUserAllowed($action) === false) { $result['errorCode'] = "priv"; $result['error'] = "You do not have permission to perform this action."; return $result; } // Check in permission protected API calls switch ($action) { // admin only // device setup case "devices/setup": $setup = new WposPosSetup($data); $result = $setup->setupDevice($result); break; // stored items // stored items case "adminconfig/get": $setupMdl = new WposPosSetup(); $result = $setupMdl->getAdminConfig($result); break; case "items/add": $adminMdl = new WposAdminItems($data); $result = $adminMdl->addStoredItem($result); break; case "items/edit": $adminMdl = new WposAdminItems($data); $result = $adminMdl->updateStoredItem($result); break; case "items/delete": $adminMdl = new WposAdminItems($data); $result = $adminMdl->deleteStoredItem($result); break; // suppliers // suppliers case "suppliers/get": $jsondata = new WposPosData(); $result = $jsondata->getSuppliers($result); break; case "suppliers/add": $adminMdl = new WposAdminItems($data); $result = $adminMdl->addSupplier($result); break; case "suppliers/edit": $adminMdl = new WposAdminItems($data); $result = $adminMdl->updateSupplier($result); break; case "suppliers/delete": $adminMdl = new WposAdminItems($data); $result = $adminMdl->deleteSupplier($result); break; // suppliers // suppliers case "stock/get": $jsondata = new WposPosData(); $result = $jsondata->getStock($result); break; case "stock/add": $stockMdl = new WposAdminStock($data); $result = $stockMdl->addStock($result); break; case "stock/set": $stockMdl = new WposAdminStock($data); $result = $stockMdl->setStockLevel($result); break; case "stock/transfer": $stockMdl = new WposAdminStock($data); $result = $stockMdl->transferStock($result); break; case "stock/history": $stockMdl = new WposAdminStock($data); $result = $stockMdl->getStockHistory($result); break; // customers // customers case "customers/add": $custMdl = new WposAdminCustomers($data); $result = $custMdl->addCustomer($result); break; case "customers/edit": $custMdl = new WposAdminCustomers($data); $result = $custMdl->updateCustomer($result); break; case "customers/delete": $custMdl = new WposAdminCustomers($data); $result = $custMdl->deleteCustomer($result); break; case "customers/contacts/add": $custMdl = new WposAdminCustomers($data); $result = $custMdl->addContact($result); break; case "customers/contacts/edit": $custMdl = new WposAdminCustomers($data); $result = $custMdl->updateContact($result); break; case "customers/contacts/delete": $custMdl = new WposAdminCustomers($data); $result = $custMdl->deleteContact($result); break; // TODO: Add to permissions // TODO: Add to permissions case "customers/setaccess": $custMdl = new WposAdminCustomers($data); $result = $custMdl->setAccess($result); break; case "customers/setpassword": $custMdl = new WposAdminCustomers($data); $result = $custMdl->setPassword($result); break; case "customers/sendreset": $custMdl = new WposAdminCustomers($data); $result = $custMdl->sendResetEmail($result); break; // End to-do // USERS // End to-do // USERS case "users/get": $data = new WposPosData(); $result = $data->getUsers($result); break; case "users/add": $adminMdl = new WposAdminItems($data); $result = $adminMdl->addUser($result); break; case "users/edit": $adminMdl = new WposAdminItems($data); $result = $adminMdl->updateUser($result); break; case "users/delete": $adminMdl = new WposAdminItems($data); $result = $adminMdl->deleteUser($result); break; case "users/disable": $setup = new WposAdminItems($data); $result = $setup->setUserDisabled($result); break; // DEVICES // DEVICES case "devices/add": $setup = new WposPosSetup($data); $result = $setup->addDevice($result); break; case "devices/edit": $setup = new WposPosSetup($data); $result = $setup->updateDevice($result); break; case "devices/delete": $setup = new WposPosSetup($data); $result = $setup->deleteDevice($result); break; case "devices/disable": $setup = new WposPosSetup($data); $result = $setup->setDeviceDisabled($result); break; // LOCATIONS // LOCATIONS case "locations/add": $setup = new WposPosSetup($data); $result = $setup->addLocation($result); break; case "locations/edit": $setup = new WposPosSetup($data); $result = $setup->updateLocationName($result); break; case "locations/delete": $setup = new WposPosSetup($data); $result = $setup->deleteLocation($result); break; case "locations/disable": $setup = new WposPosSetup($data); $result = $setup->setLocationDisabled($result); break; // SALES (All transactions) // SALES (All transactions) case "sales/delete": $aSaleMdl = new WposTransactions($data); $result = $aSaleMdl->deleteSale($result); break; case "sales/deletevoid": $aSaleMdl = new WposTransactions($data); $result = $aSaleMdl->removeVoidRecord($result); break; case "sales/adminvoid": // the admin add void method, only requires sale id and reason $aSaleMdl = new WposTransactions($data); $result = $aSaleMdl->voidSale($result); break; // INVOICES // INVOICES case "invoices/get": $invMdl = new WposInvoices($data); $result = $invMdl->getInvoices($result); break; case "invoices/add": $invMdl = new WposInvoices($data); $result = $invMdl->createInvoice($result); break; case "invoices/edit": $invMdl = new WposInvoices($data); $result = $invMdl->updateInvoice($result); break; case "invoices/delete": $invMdl = new WposInvoices($data); $result = $invMdl->removeInvoice($result); break; case "invoices/items/add": $invMdl = new WposInvoices($data); $result = $invMdl->addItem($result); break; case "invoices/items/edit": $invMdl = new WposInvoices($data); $result = $invMdl->updateItem($result); break; case "invoices/items/delete": $invMdl = new WposInvoices($data); $result = $invMdl->removeItem($result); break; case "invoices/payments/add": $invMdl = new WposInvoices($data); $result = $invMdl->addPayment($result); break; case "invoices/payments/edit": $invMdl = new WposInvoices($data); $result = $invMdl->updatePayment($result); break; case "invoices/payments/delete": $invMdl = new WposInvoices($data); $result = $invMdl->removePayment($result); break; case "invoices/history/get": $invMdl = new WposTransactions($data); $result = $invMdl->getTransactionHistory($result); break; case "invoices/generate": $invMdl = new WposTransactions(null, $_REQUEST['id'], false); $invMdl->generateInvoice(); break; case "invoices/email": $invMdl = new WposTransactions($data); $result = $invMdl->emailInvoice($result); break; // STATS // STATS case "stats/general": // general overview stats $statsMdl = new WposAdminStats($data); $result = $statsMdl->getOverviewStats($result); break; case "stats/takings": // account takings stats, categorized by payment method $statsMdl = new WposAdminStats($data); $result = $statsMdl->getCountTakingsStats($result); break; case "stats/itemselling": // whats selling, grouped by stored items $statsMdl = new WposAdminStats($data); $result = $statsMdl->getWhatsSellingStats($result); break; case "stats/supplyselling": // whats selling, grouped by suppliers $statsMdl = new WposAdminStats($data); $result = $statsMdl->getWhatsSellingStats($result, true); break; case "stats/stock": // current stock levels $statsMdl = new WposAdminStats($data); $result = $statsMdl->getStockLevels($result); break; case "stats/devices": // whats selling, grouped by stored items $statsMdl = new WposAdminStats($data); $result = $statsMdl->getDeviceBreakdownStats($result); break; case "stats/locations": // whats selling, grouped by stored items $statsMdl = new WposAdminStats($data); $result = $statsMdl->getDeviceBreakdownStats($result, 'location'); break; case "stats/users": // whats selling, grouped by stored items $statsMdl = new WposAdminStats($data); $result = $statsMdl->getDeviceBreakdownStats($result, 'user'); break; case "stats/tax": // whats selling, grouped by stored items $statsMdl = new WposAdminStats($data); $result = $statsMdl->getTaxStats($result); break; // GRAPH // GRAPH case "graph/general": // like the general stats, but in graph form/time. $graphMdl = new WposAdminGraph($data); $result = $graphMdl->getOverviewGraph($result); break; case "graph/takings": // like the general stats, but in graph form/time. $graphMdl = new WposAdminGraph($data); $result = $graphMdl->getMethodGraph($result); break; case "graph/devices": // like the general stats, but in graph form/time. $graphMdl = new WposAdminGraph($data); $result = $graphMdl->getDeviceGraph($result); break; case "graph/locations": // like the general stats, but in graph form/time. $graphMdl = new WposAdminGraph($data); $result = $graphMdl->getLocationGraph($result); break; // Admin/Global Config // Admin/Global Config case "settings/get": $configMdl = new WposAdminSettings(); $configMdl->setName($data->name); $result = $configMdl->getSettings($result); break; case "settings/general/get": $configMdl = new WposAdminSettings(); $configMdl->setName("general"); $result = $configMdl->getSettings($result); break; case "settings/pos/get": $configMdl = new WposAdminSettings(); $configMdl->setName("pos"); $result = $configMdl->getSettings($result); break; case "settings/invoice/get": $configMdl = new WposAdminSettings(); $configMdl->setName("invoice"); $result = $configMdl->getSettings($result); break; case "settings/set": $configMdl = new WposAdminSettings($data); $result = $configMdl->saveSettings($result); break; case "settings/general/set": $configMdl = new WposAdminSettings($data); $configMdl->setName("general"); $result = $configMdl->saveSettings($result); break; case "settings/pos/set": $configMdl = new WposAdminSettings($data); $configMdl->setName("pos"); $result = $configMdl->saveSettings($result); break; case "settings/invoice/set": $configMdl = new WposAdminSettings($data); $configMdl->setName("invoice"); $result = $configMdl->saveSettings($result); break; case "settings/google/authinit": GoogleIntegration::initGoogleAuth(); break; case "settings/google/authremove": GoogleIntegration::removeGoogleAuth(); break; case "settings/xero/oauthinit": XeroIntegration::initXeroAuth(); break; case "settings/xero/oauthcallback": XeroIntegration::processCallbackAuthCode(); break; case "settings/xero/oauthremove": XeroIntegration::removeXeroAuth(); break; case "settings/xero/configvalues": $result = XeroIntegration::getXeroConfigValues($result); break; case "settings/xero/export": $result = XeroIntegration::exportXeroSales($data->stime, $data->etime); break; case "node/status": $Sserver = new WposSocketControl(); $result = $Sserver->isServerRunning($result); break; case "node/start": $Sserver = new WposSocketControl(); $result = $Sserver->startSocketServer($result); break; case "node/stop": $Sserver = new WposSocketControl(); $result = $Sserver->stopSocketServer($result); break; case "node/restart": $Sserver = new WposSocketControl(); $result = $Sserver->restartSocketServer($result); break; case "db/backup": $util = new WposAdminUtilities(); $util->backUpDatabase(); break; case "logs/list": $result['data'] = Logger::ls(); break; case "logs/read": $result['data'] = Logger::read($data->filename); break; case "file/upload": if (isset($_FILES['file'])) { $uploaddir = 'docs'; $newpath = $uploaddir . DIRECTORY_SEPARATOR . basename($_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . $_SERVER['APP_ROOT'] . $newpath) !== false) { $result['data'] = ["path" => "/" . $newpath]; } else { $result['error'] = "There was an error uploading the file " . $newpath; } } else { $result['error'] = "No file selected"; } break; // device message // device message case "message/send": $socket = new WposSocketIO(); if ($data->device === null) { if (($error = $socket->sendBroadcastMessage($data->message)) !== true) { $result['error'] = $error; } } else { $devid = intval($data->device); $devices = new stdClass(); $devices->{$devid} = $devid; if (($error = $socket->sendMessageToDevices($devices, $data->message)) !== true) { $result['error'] = $error; } } break; // device reset // device reset case "device/reset": $socket = new WposSocketIO(); if ($data->device === null) { if (($error = $socket->sendResetCommand()) !== true) { $result['error'] = $error; } } else { $devid = intval($data->device); $devices = new stdClass(); $devices->{$devid} = $devid; if (($error = $socket->sendResetCommand($devices)) !== true) { $result['error'] = $error; } } break; default: $result["error"] = "Action not defined: " . $action; break; } return $result; }