private function deleteInvoice() { if ($this->invMdl->remove($this->id) === false) { return false; } $itemMdl = new SaleItemsModel(); if ($itemMdl->removeBySale($this->id) === false) { return false; } $payMdl = new SalePaymentsModel(); if ($payMdl->removeBySale($this->id) === false) { return false; } $voidMdl = new SaleVoidsModel(); if ($voidMdl->removeBySale($this->id) === false) { return false; } $histMdl = new TransHistModel(); if ($histMdl->removeBySale($this->id) === false) { return false; } return true; }
/** * 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; }
/** * Get grouped sales stats for the current range, grouped by user, device or location * @param $result * @param string $type * @return mixed */ public function getDeviceBreakdownStats($result, $type = 'device') { $stats = []; $salesMdl = new TransactionsModel(); $voidMdl = new SaleVoidsModel(); // check if params set, if not set defaults $stime = isset($this->data->stime) ? $this->data->stime : strtotime('-1 week') * 1000; $etime = isset($this->data->etime) ? $this->data->etime : time() * 1000; // setup default object $defaults = new stdClass(); $defaults->refs = ''; $defaults->refundrefs = ''; $defaults->voidtotal = 0; $defaults->voidnum = 0; $defaults->saletotal = 0; $defaults->salenum = 0; $defaults->refundtotal = 0; $defaults->refundnum = 0; // get non voided sales if (($sales = $salesMdl->getGroupedTotals($stime, $etime, 3, false, $type)) !== false) { foreach ($sales as $sale) { if ($sale['groupid'] == null) { $sale['name'] = "Admin Dash"; } if (!isset($stats[$sale['groupid']])) { $stats[$sale['groupid']] = clone $defaults; $stats[$sale['groupid']]->name = $sale['name']; } $stats[$sale['groupid']]->refs = $sale['refs']; $stats[$sale['groupid']]->salerefs = $sale['refs']; $stats[$sale['groupid']]->saletotal = $sale['stotal']; $stats[$sale['groupid']]->salenum = $sale['snum']; } } else { $result['error'] = "Error getting sales: " . $salesMdl->errorInfo; } // get voided sales if (($voids = $salesMdl->getGroupedTotals($stime, $etime, 3, true, $type)) !== false) { foreach ($voids as $void) { if ($void['groupid'] == null) { $sale['name'] = "Admin Dash"; } if (!isset($stats[$void['groupid']])) { $stats[$void['groupid']] = clone $defaults; $stats[$void['groupid']]->name = $void['name']; } $stats[$void['groupid']]->refs .= ($stats[$void['groupid']]->refs == '' ? '' : ',') . $void['refs']; $stats[$void['groupid']]->voidrefs = $void['refs']; $stats[$void['groupid']]->voidtotal = $void['stotal']; $stats[$void['groupid']]->voidnum = $void['snum']; } } else { $result['error'] = "Error getting voided sales: " . $salesMdl->errorInfo; } // get refunds if (($refunds = $voidMdl->getGroupedTotals($stime, $etime, false, $type)) !== false) { foreach ($refunds as $refund) { if ($refund['groupid'] == null) { $sale['name'] = "Admin Dash"; } if (!isset($stats[$refund['groupid']])) { $stats[$refund['groupid']] = clone $defaults; $stats[$refund['groupid']]->name = $refund['name']; } $stats[$refund['groupid']]->refs .= ($stats[$refund['groupid']]->refs == '' ? '' : ',') . $refund['refs']; $stats[$refund['groupid']]->refundrefs = $refund['refs']; $stats[$refund['groupid']]->refundtotal = $refund['stotal']; $stats[$refund['groupid']]->refundnum = $refund['snum']; } } else { $result['error'] = "Error getting refunds: " . $voidMdl->errorInfo; } // calc total takings for each device/location foreach ($stats as $key => $stat) { $stats[$key]->balance = number_format($stat->saletotal - $stat->refundtotal, 2, '.', ''); } // include totals if requested if ($this->data->totals == true) { $result = $this->getOverviewStats($result); $stats["Totals"] = $result['data']; } $result['data'] = $stats; 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; }
/** * Removes the transaction record and all associated records * @param $saleid * @return bool|int Returns false on failure or number of rows affected on success */ public function remove($saleid) { $sql = "DELETE FROM `sales` WHERE `id` = :saleid"; $placeholders = [":saleid" => $saleid]; // Remove associated records $saleItemsMdl = new SaleItemsModel(); $salePaymentsMdl = new SalePaymentsModel(); $saleVoidMdl = new SaleVoidsModel(); if (($result = $saleVoidMdl->removeBySale($saleid)) !== false) { if (($result = $salePaymentsMdl->removeBySale($saleid)) !== false) { $result = $saleItemsMdl->removeBySale($saleid); } } if ($result !== false) { return $this->delete($sql, $placeholders); } else { return $result; } }