public function updatePayment($result) { // validate json $jsonval = new JsonValidate($this->data, '{"id":1, "paymentid":1, "method":"", "amount":1, "processdt":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } // update payment record $payMdl = new SalePaymentsModel(); if (($payid = $payMdl->edit($this->data->paymentid, $this->data->method, $this->data->amount, $this->data->processdt)) === false) { $result['error'] = "Could not insert item record: " . $payMdl->errorInfo; return $result; } foreach ($this->invoice->payments as $key => $item) { if ($this->data->paymentid == $item->id) { $this->data->id = $this->data->paymentid; unset($this->data->paymentid); $this->invoice->payments[$key] = $this->data; break; } } // Update invoice totals $this->calculateInvoice(); // update invoice data if ($this->saveInvoiceData() === false) { $result['error'] = "Could not commit invoice data: " . $this->invMdl->errorInfo; return $result; } else { // Create transaction history record WposTransactions::addTransactionHistory($this->id, $_SESSION['userId'], "Modified", "Payment Modified"); // log data Logger::write("Invoice payment modified for invoice id: " . $this->id, "INVOICE", json_encode($this->data)); } $result['data'] = $this->invoice; return $result; }
/** * Set customer password * @param $result * @return mixed */ public function setPassword($result) { $jsonval = new JsonValidate($this->data, '{"id":1, "hash":""}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } $custMdl = new CustomerModel(); $res = $custMdl->editAuth($this->data->id, $this->data->hash, 1, 0); if ($res === false) { $result['error'] = "Could not set customer account status: " . $custMdl->errorInfo; } return $result; }
/** * Process refund & void records only (the sale already has a ID) * @param $result * @return mixed */ public function insertVoid($result) { $this->salesMdl = new SalesModel(); $hasrefund = $this->refunddata !== null ? true : false; $hasvoid = $this->voiddata !== null ? true : false; $status = ($hasrefund or $hasvoid) ? $hasvoid ? 3 : 2 : 1; $newtran = true; // validate values if ($hasvoid) { $jsonval = new JsonValidate($this->voiddata, '{"userid":1, "deviceid":1, "locationid":1, "reason":"", "processdt":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } } if ($hasrefund) { foreach ($this->refunddata as $refund) { $jsonval = new JsonValidate($refund, '{"userid":1, "deviceid":1, "locationid":1, "reason":"", "processdt":1, "items":"[", "method":"", "amount":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } } } // processing for the current transaction?, if not we need to fetch the record from the database and update the JSON object if ($this->jsonobj == null) { $newtran = false; // void/refund of an old transaction // get record with the current ref if (($dbresult = $this->salesMdl->getByRef($this->ref)) !== false) { // load sales json vars $this->extractDbData($dbresult[0]['data']); } else { $result["error"] = "Could not find record in the database to update."; return $result; } // update json sale data with new void/refund data if ($hasrefund) { $this->jsonobj->refunddata = $this->refunddata; } if ($hasvoid) { $this->jsonobj->voiddata = $this->voiddata; } } $this->jsonobj->status = $status; // check for void record and insert $result = $this->insertVoidRecords($hasrefund, $hasvoid, $result); if ($result["error"] == "OK") { // update database with new json data and void indicator if ($this->salesMdl->edit(null, $this->ref, json_encode($this->jsonobj), $status) !== false) { if (!$newtran) { $result['data'] = $this->jsonobj; // only need to update if an old transaction // broadcast to other devices $this->broadcastSale($this->deviceid, true); // add flag indicating updated sale (for admin dashboard) } } else { $result["error"] = $this->salesMdl->errorInfo; } } return $result; }
/** * Generate invoice for the specified transaction * @param $result * @return mixed */ public function emailInvoice($result) { // validate json $jsonval = new JsonValidate($this->data, '{"id":1, "to":""}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } if (!$this->trans) { if ($this->loadTransaction() === false) { die("Failed to load the transaction!"); } } // Generate Invoice PDF $html = $this->generateInvoiceHtml(); $pdf = $this->convertToPdf($html, 0); $attachment = [$pdf, "Invoice #" . $this->trans->ref . ".pdf"]; $subject = isset($this->data->subject) ? $this->data->subject : "Invoice #" . $this->trans->ref . " Attached"; $message = isset($this->data->message) && $this->data->message !== "" ? $this->data->message : "Please find the attached invoice"; $cc = isset($this->data->cc) ? $this->data->cc : null; $bcc = isset($this->data->bcc) ? $this->data->bcc : null; // Constuct & send email $email = new WposMail(); $emlresult = $email->sendHtmlEmail($this->data->to, $subject, $message, $cc, $bcc, $attachment); if ($emlresult !== true) { $result['error'] = $emlresult; } else { // Create transaction history record WposTransactions::addTransactionHistory($this->trans->id, $_SESSION['userId'], "Emailed", "Invoice emailed to: " . $this->data->to . ($cc != null ? "," . $cc : "") . ($bcc != null ? "," . $bcc : "")); } return $result; }
/** * Update a locations name * @param $result * @return mixed */ public function updateLocationName($result) { // validate input $jsonval = new JsonValidate($this->data, '{"locid":1, "locname":""}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } $locMdl = new LocationsModel(); if ($locMdl->edit($this->locationId, $this->locationName) !== false) { $result['data'] = true; // log data Logger::write("Location updated", "CONFIG", json_encode($this->data)); } else { $result['error'] = "Could not update the location"; } return $result; }
/** * Update a tax rule * @param $result * @return mixed */ public function updateTaxItem($result) { $jsonval = new JsonValidate($this->data, '{"name":"", "type":"", "value":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } $this->data->multiplier = WposAdminItems::calculateTaxMultiplier($this->data->value); $taxItemMdl = new TaxItemsModel(); $qresult = $taxItemMdl->edit($this->data->id, $this->data->name, $this->data->type, $this->data->value, $this->data->multiplier); if ($qresult === false) { $result['error'] = "Could not edit the tax item: " . $taxItemMdl->errorInfo; } else { $result['data'] = $this->data; $this->broadcastTaxUpdate(); // log data Logger::write("Tax item updated with id:" . $this->data->id, "TAX", json_encode($this->data)); } return $result; }
/** * Update user * @param $result * @return mixed */ public function updateUser($result) { // prevent updating of master admin username if ($this->data->id == 1 && !isset($this->data->pass)) { $result['error'] = "Only the master admin password may be updated."; return $result; } // validate input $jsonval = new JsonValidate($this->data, '{"id":1, "username":"", "admin":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } $authMdl = new AuthModel(); if ($this->data->id == 1) { // Only rhe admin users password can be updated $qresult = $authMdl->edit($this->data->id, $this->data->username, $this->data->pass); unset($this->data->permissions); unset($this->data->admin); } else { $dupitems = $authMdl->get(0, 0, null, $this->data->username); if (sizeof($dupitems) > 0) { $dupitem = $dupitems[0]; if ($dupitem['id'] != $this->data->id) { $result['error'] = "The username specified is already taken"; return $result; } } // generate permissions object $permObj = ["sections" => $this->data->permissions, "apicalls" => []]; foreach ($this->data->permissions as $key => $value) { switch ($key) { case "access": if ($value != "no") { $permObj['apicalls'][] = "adminconfig/get"; } break; case "dashboard": if ($value == "both" || $value == "standard") { $permObj['apicalls'] = array_merge($permObj['apicalls'], $this->permissionMap['readapicalls']['dashboard']); } if ($value == "both" || $value == "realtime") { $permObj['apicalls'] = array_merge($permObj['apicalls'], $this->permissionMap['readapicalls']['realtime']); } break; default: switch ($value) { case 2: // add write api calls if (isset($this->permissionMap['editapicalls'][$key])) { $permObj['apicalls'] = array_merge($permObj['apicalls'], $this->permissionMap['editapicalls'][$key]); } case 1: // add read api calls if (isset($this->permissionMap['readapicalls'][$key])) { $permObj['apicalls'] = array_merge($permObj['apicalls'], $this->permissionMap['readapicalls'][$key]); } break; } } } if ($this->data->pass == "") { $qresult = $authMdl->edit($this->data->id, $this->data->username, null, $this->data->admin, json_encode($permObj)); } else { $qresult = $authMdl->edit($this->data->id, $this->data->username, $this->data->pass, $this->data->admin, json_encode($permObj)); } } if ($qresult === false) { $result['error'] = "Could not update the user"; } else { $result['data'] = true; // log data unset($this->data->pass); Logger::write("User updated with id:" . $this->data->id, "USER", json_encode($this->data)); } return $result; }
/** * Add stock to a location * @param $result * @return mixed */ public function addStock($result) { // validate input $jsonval = new JsonValidate($this->data, '{"storeditemid":1, "locationid":1, "amount":">=1"}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } // create history record for added stock if ($this->createStockHistory($this->data->storeditemid, $this->data->locationid, 'Stock Added', $this->data->amount) === false) { $result['error'] = "Could not create stock history record"; return $result; } // add stock amount to new location if ($this->incrementStockLevel($this->data->storeditemid, $this->data->locationid, $this->data->amount, false) === false) { $result['error'] = "Could not add stock to the new location"; return $result; } // Success; log data Logger::write("Stock Added", "STOCK", json_encode($this->data)); return $result; }
/** * Generate plot data using the specified type * @param $result * @param $graphtype * @return mixed */ private function getGraph($result, $graphtype) { // validate input $jsonval = new JsonValidate($this->data, '{"stime":1, "etime":1, "interval":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } // Initialize the stats object $stats = new WposAdminStats(null); $graph = []; $serieslist = []; $interval = isset($this->data->interval) ? $this->data->interval : 86400000; // default interval is one day $curstime = isset($this->data->stime) ? $this->data->stime : strtotime('-1 week') * 1000; $curetime = intval($curstime) + intval($interval); $stopetime = isset($this->data->etime) ? $this->data->etime : time() * 1000; $tempstats = null; while ($curstime <= $stopetime) { $stats->setRange($curstime, $curetime); switch ($graphtype) { case 1: $tempstats = $stats->getOverviewStats($result); break; case 2: $tempstats = $stats->getCountTakingsStats($result); break; case 3: $tempstats = $stats->getDeviceBreakdownStats($result); break; case 4: $tempstats = $stats->getDeviceBreakdownStats($result, 'location'); break; } if ($tempstats['error'] == "OK") { // put into series list foreach ($tempstats['data'] as $key => $value) { $serieslist[$key] = $key; } // put into array $graph[$curstime] = $tempstats['data']; } else { $result['error'] .= $tempstats['error']; break; } // move to the next segment $curstime += $interval; $curetime += $interval; } // if it's not the general graph we need to loop through and fill in null data if ($graphtype != 1) { $defaultobj = new stdClass(); $defaultobj->balance = 0; // loop through each series value and add 0 values for null data foreach ($graph as $ykey => $yvals) { //$result['error'].="\n".json_encode($yvals); foreach ($serieslist as $value) { // use serieslist to spot null values if ($yvals[$value] == null || empty($yvals)) { // check if series key exists in current timeset //$result['error'].="\nInserting default"; $yvals[$value] = $defaultobj; $graph[$ykey] = $yvals; } } } } $result['data'] = $graph; return $result; }
/** * Update the current customers details * @param $result * @return mixed */ public function saveCustomerDetails($result) { // Safety check if (!isset($_SESSION['cust_id'])) { $result['error'] = "Customer ID not found in current session"; return $result; } // input validation $jsonval = new JsonValidate($this->data, '{"name":"", "email":"@", "address":"", "suburb":"", "postcode":"", "state":"", "country":""}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } if (!$this->data->phone && !$this->data->mobile) { $result['error'] = "At least one contact phone number must be specified."; return $result; } // set id $this->data->id = $_SESSION['cust_id']; $dres = WposAdminCustomers::updateCustomerData($this->data); if ($dres === false) { $result['error'] = "Failed to update customer details."; } return $result; }