/** * Add any new void records for the transaction * @param $hasrefund * @param $hasvoid * @param $result * @return mixed */ private function insertVoidRecords($hasrefund, $hasvoid, $result) { $voidMdl = new SaleVoidsModel(); // update new refund records if ($hasrefund) { $saleItemsMdl = new SaleItemsModel(); foreach ($this->refunddata as $refund) { // Check if record has already been processed if (!$voidMdl->recordExists($this->id, $refund->processdt)) { $this->deviceid = $refund->deviceid; // set device id for the broadcast function $voidMdl->create($this->id, $refund->userid, $refund->deviceid, $refund->locationid, $refund->reason, $refund->method, $refund->amount, json_encode($refund->items), 0, $refund->processdt); // Increment refunded quantities in the sale_items table foreach ($refund->items as $item) { $saleItemsMdl->incrementQtyRefunded($this->id, $item->ref, $item->numreturned); } // Create transaction history record WposTransactions::addTransactionHistory($this->id, isset($_SESSION['userId']) ? $_SESSION['userId'] : 0, "Refunded", "Sale refunded"); // log data Logger::write("Refund processed with ref: " . $this->ref, "REFUND", json_encode($refund)); } } } if ($hasvoid) { // Check if record has already been processed if (!$voidMdl->recordExists($this->id, $this->voiddata->processdt)) { $this->deviceid = $this->voiddata->deviceid; // set device id for the broadcast function $id = $voidMdl->create($this->id, $this->voiddata->userid, $this->voiddata->deviceid, $this->voiddata->locationid, $this->voiddata->reason, "", 0, 0, 1, $this->voiddata->processdt); if (!$id > 0) { $result["error"] .= $voidMdl->errorInfo; } else { // return stock to original sale location if (sizeof($this->jsonobj->items) > 0) { $wposStock = new WposAdminStock(); foreach ($this->jsonobj->items as $item) { if ($item->sitemid > 0) { $wposStock->incrementStockLevel($item->sitemid, $this->jsonobj->locid, $item->qty, false); } } } // Create transaction history record WposTransactions::addTransactionHistory($this->id, isset($_SESSION['userId']) ? $_SESSION['userId'] : 0, "Voided", "Sale voided"); // log data Logger::write("Sale voided with ref: " . $this->ref, "VOID", json_encode($this->voiddata)); } } } return $result; }
/** * Void the sales specified by ID * @param $result * @return mixed */ public function voidSale($result) { // validate input if (!is_numeric($this->data->id)) { $result['error'] = "A valid id must be supplied"; return $result; } // insert entry $voidMdl = new SaleVoidsModel(); $saleMdl = new SalesModel(); // contruct voiddata object $voiddata = new stdClass(); $voiddata->processdt = time() * 1000; // convert to milliseconds $voiddata->userid = $_SESSION['userId']; $voiddata->deviceid = 0; // id of zero corresponds to "admin dashboard", which is added into the JSON array sent to the client with WposSetup/getConfig $voiddata->locationid = 0; $voiddata->reason = $this->data->reason; // insert void record if ($voidMdl->create($this->data->id, 0, 0, 0, $this->data->reason, "", 0, 0, 1, $voiddata->processdt)) { // get existing sales data if (($sale = $saleMdl->getById($this->data->id)) !== false) { // add voiddata to json object $data = json_decode($sale[0]['data']); $data->voiddata = $voiddata; if (!$saleMdl->edit($this->data->id, null, json_encode($data), 3)) { $result['error'] = "Could not update the sales record: " . $saleMdl->errorInfo; } else { $result['data'] = $data; // return stock to original sale location if (sizeof($data->items) > 0) { $wposStock = new WposAdminStock(); foreach ($data->items as $item) { if ($item->sitemid > 0) { $wposStock->incrementStockLevel($item->sitemid, $data->locid, $item->qty, false); } } } // Create transaction history record WposTransactions::addTransactionHistory($this->data->id, $_SESSION['userId'], "Voided", "Transaction Voided"); // Success; log data Logger::write("Sale voided with ref:" . $data->ref, "VOID"); } } else { $result['error'] = "Could not retrieve the sales record: " . $saleMdl->errorInfo; } } else { $result['error'] = "Could not insert void record: " . $voidMdl->errorInfo; } return $result; }