/** * 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; }
/** * 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; }