/** * Remove all transaction records associated with a sale * @return bool */ private function removeTransactionRecords() { $itemsMdl = new SaleItemsModel(); $payMdl = new SalePaymentsModel(); if ($this->salesMdl->remove($this->id) !== false) { if ($payMdl->removeBySale($this->id) !== false) { if ($itemsMdl->removeBySale($this->id) !== false) { return true; } } } return false; }
/** * Searches sales for the given reference. * @param $searchdata * @param $result * @return mixed Returns sales that match the specified ref. */ public function searchSales($searchdata, $result) { $salesMdl = new SalesModel(); $dbSales = $salesMdl->get(0, 0, $searchdata->ref, null, null, null, null, true); if (is_array($dbSales)) { $sales = []; foreach ($dbSales as $sale) { $jsonObj = json_decode($sale['data'], true); $sales[$sale['ref']] = $jsonObj; } $result['data'] = $sales; } else { if ($dbSales === false) { $result['error'] = $salesMdl->errorInfo; } } 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; }
<?php session_start(); include 'protected/config/db_config.php'; include 'protected/config/html_config.php'; include 'protected/library/validation_library.php'; include 'protected/controllers/index.php'; include 'protected/models/admin-sales-report.php'; $db = new db_config(); $formelem = new FormElem(); $brand_name = $_SESSION['brand_name']; $SalesModel = new SalesModel(); $connect = $db->connect(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>Sales Report</title> <!-- Bootstrap Core CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- Custom CSS -->