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; }
/** * 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; }
/** * 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; }
/** * Get tax statistics from the current range * @param $result * @return mixed */ public function getTaxStats($result) { $stats = []; $itemsMdl = new SaleItemsModel(); // 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; if (is_array($taxtotals = $itemsMdl->getTaxTotals($stime, $etime, true, $this->data->type))) { foreach ($taxtotals as $tax) { $stats[$tax['taxid']] = new stdClass(); $stats[$tax['taxid']]->refs = $tax['refs']; $stats[$tax['taxid']]->name = $tax['taxname']; $stats[$tax['taxid']]->qtyitems = $tax['qtyitems']; //$stats[$tax['taxid']]->salebalance = number_format($tax['saletotal']-$tax['refundtotal'], 2); $stats[$tax['taxid']]->saletotal = number_format($tax['saletotal'], 2); $stats[$tax['taxid']]->refundtotal = number_format($tax['refundtotal'], 2); if ($tax['taxid'] == 1) { $stats[$tax['taxid']]->saletax = number_format(0.0, 2); $stats[$tax['taxid']]->refundtax = number_format(0.0, 2); } else { $taxMdl = new TaxItemsModel(); $taxdata = $taxMdl->get($tax['taxid']); if ($taxdata === false) { $result['error'] = "Could not calculate tax"; return $result; } $taxdiv = $taxdata[0]['divisor']; $stats[$tax['taxid']]->saletax = round($tax['saletotal'] / $taxdiv, 2); $stats[$tax['taxid']]->refundtax = round($tax['refundtotal'] / $taxdiv, 2); } $stats[$tax['taxid']]->balance = number_format($stats[$tax['taxid']]->saletax - $stats[$tax['taxid']]->refundtax, 2); $stats[$tax['taxid']]->saletax = number_format($stats[$tax['taxid']]->saletax, 2); $stats[$tax['taxid']]->refundtax = number_format($stats[$tax['taxid']]->refundtax, 2); } // Get cash rounding total $roundtotals = $itemsMdl->getRoundingTotal($stime, $etime); if ($roundtotals !== false) { $stats[0] = new stdClass(); $stats[0]->refs = $roundtotals[0]['refs']; $stats[0]->name = "Cash Rounding"; $stats[0]->qty = $roundtotals[0]['num']; $stats[0]->total = $roundtotals[0]['total']; } else { $result['error'] = $itemsMdl->errorInfo; } } else { $result['error'] = $itemsMdl->errorInfo; } $result['data'] = $stats; 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; } }
/** * Get tax statistics from the current range * @param $result * @return mixed */ public function getTaxStats($result) { $stats = []; $itemsMdl = new SaleItemsModel(); // 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; if (is_array($saleitems = $itemsMdl->getTotalsRange($stime, $etime, true, $this->data->type))) { $taxMdl = new TaxItemsModel(); $taxdata = $taxMdl->get(); $taxes = []; foreach ($taxdata as $value) { $taxes[$value['id']] = (object) $value; } foreach ($saleitems as $saleitem) { $itemtax = json_decode($saleitem['tax']); if ($itemtax->total == 0) { if (!array_key_exists(-1, $stats)) { $stats[-1] = new stdClass(); $stats[-1]->refs = []; $stats[-1]->name = "Untaxed"; $stats[-1]->qtyitems = 0; $stats[-1]->saletotal = 0; $stats[-1]->refundtotal = 0; $stats[-1]->saletax = 0; $stats[-1]->refundtax = 0; } if (!in_array($saleitem['ref'], $stats[-1]->refs)) { $stats[-1]->refs[] = $saleitem['ref']; } $stats[-1]->qtyitems += $saleitem['qty']; $stats[-1]->saletotal += $saleitem['itemtotal']; $stats[-1]->refundtotal += $saleitem['refundtotal']; } else { // subtotal excludes tax, factors in discount $discountedtax = $saleitem['discount'] > 0 ? round($itemtax->total - $itemtax->total * ($saleitem['discount'] / 100), 2) : $itemtax->total; //echo($discountedtax); $itemsubtotal = $saleitem['itemtotal'] - $discountedtax; $refundsubtotal = $saleitem['refundtotal'] - round($discountedtax / $saleitem['qty'] * $saleitem['refundqty'], 2); foreach ($itemtax->values as $key => $value) { if (!array_key_exists($key, $stats)) { $stats[$key] = new stdClass(); $stats[$key]->refs = []; $stats[$key]->name = isset($taxes[$key]) ? $taxes[$key]->name : "Unknown"; $stats[$key]->qtyitems = 0; $stats[$key]->saletotal = 0; $stats[$key]->refundtotal = 0; //$stats[$key]->saletax = 0; //$stats[$key]->refundtax = 0; } if (!in_array($saleitem['ref'], $stats[$key]->refs)) { $stats[$key]->refs[] = $saleitem['ref']; } $stats[$key]->qtyitems += $saleitem['qty']; $stats[$key]->saletotal += $itemsubtotal; // subtotal excludes tax, factors in discount $stats[$key]->refundtotal += $refundsubtotal; //$stats[$key]->saletax += $saleitem['discount']>0 ? round($value - ($value*($saleitem['discount']/100)), 2) : $value; // $stats[$key]->refundtax += $saleitem['discount']>0 ? (round($value/($saleitem['discount']/100), 2)/$saleitem['qty'])*$saleitem['refundqty']: ($value/$saleitem['qty'])*$saleitem['refundqty']; } } } foreach ($stats as $key => $value) { $taxitems = WposAdminUtilities::getTaxTable()['items']; $stats[$key]->saletax = round($taxitems[$key]['multiplier'] * $stats[$key]->saletotal, 2); $stats[$key]->refundtax = round($taxitems[$key]['multiplier'] * $stats[$key]->refundtotal, 2); $stats[$key]->balance = number_format($stats[$key]->saletax - $stats[$key]->refundtax, 2); } // Get cash rounding total $roundtotals = $itemsMdl->getRoundingTotal($stime, $etime); if ($roundtotals !== false) { $stats[0] = new stdClass(); $stats[0]->refs = $roundtotals[0]['refs']; $stats[0]->name = "Cash Rounding"; $stats[0]->qty = $roundtotals[0]['num']; $stats[0]->total = $roundtotals[0]['total']; } else { $result['error'] = $itemsMdl->errorInfo; } } else { $result['error'] = $itemsMdl->errorInfo; } $result['data'] = $stats; return $result; }