public function addItem($result) { // validate json $jsonval = new JsonValidate($this->data, '{"id":1, "sitemid":1, "qty":1, "name":"", "desc":"~", "taxid":1, "tax":"{", "unit":1, "price":1}'); if (($errors = $jsonval->validate()) !== true) { $result['error'] = $errors; return $result; } // insert item record $itemMdl = new SaleItemsModel(); if (($itemid = $itemMdl->create($this->data->id, $this->data->sitemid, 0, $this->data->qty, $this->data->name, $this->data->desc, $this->data->taxid, $this->data->tax, $this->data->unit, $this->data->price)) === false) { $result['error'] = "Could not insert item record: " . $itemMdl->errorInfo; return $result; } // set item in json $this->data->id = $itemid; $this->invoice->items[] = $this->data; // 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 { // decrement stock if ($this->data->sitemid > 0) { $wposStock = new WposAdminStock(); $wposStock->incrementStockLevel($this->data->sitemid, 0, $this->data->qty, true); } // Create transaction history record WposTransactions::addTransactionHistory($this->id, $_SESSION['userId'], "Modified", "Item Added"); // log data Logger::write("Invoice item added for invoice id: " . $this->id, "INVOICE", json_encode($this->data)); } $result['data'] = $this->invoice; return $result; }
/** * Insert transaction item records * @return bool */ private function insertTransactionItems() { $itemsMdl = new SaleItemsModel(); //$stockMdl = new StockModel(); $wposStock = new WposAdminStock(); foreach ($this->jsonobj->items as $key => $item) { if (!($res = $itemsMdl->create($this->id, $item->sitemid, $item->ref, $item->qty, $item->name, $item->desc, $item->taxid, $item->tax, $item->unit, $item->price))) { $this->itemErr = $itemsMdl->errorInfo; return false; } // decrement stock level if ($item->sitemid > 0) { /*$stockMdl->incrementStockLevel($item->sitemid, $this->jsonobj->locid, $item->qty, true);*/ $wposStock->incrementStockLevel($item->sitemid, $this->jsonobj->locid, $item->qty, true); } $this->jsonobj->items[$key]->id = $res; } return true; }