public function importInvoice($result) { $this->invoice = $this->data; if (!isset($this->invoice->devid)) { $this->invoice->devid = 0; } if (!isset($this->invoice->locid)) { $this->invoice->locid = 0; } // Get invoice totals $this->calculateInvoice(); $this->invoice->userid = isset($this->invoice->userid) ? $this->invoice->userid : $_SESSION['userId']; // insert main transaction record if ($this->invMdl->import($this->invoice->id, $this->invoice->ref, "manual", json_encode($this->invoice), 0, $this->invoice->userid, $this->invoice->custid, $this->invoice->discount, $this->invoice->total, $this->invoice->balance, $this->invoice->processdt, $this->invoice->duedt) === false) { $result['error'] = "Could not insert invoice record: " . $this->invMdl->errorInfo; return $result; } // add sale id and processdt to json $this->id = $this->invoice->id; $this->invoice->dt = date("Y-m-d H:i:s"); $this->invoice->userid = $_SESSION['userId']; // insert items if (isset($this->invoice->items) && sizeof($this->invoice->items) > 0) { $itemMdl = new SaleItemsModel(); foreach ($this->invoice->items as $item) { $itemid = $itemMdl->import($item->id, $this->id, $item->sitemid, 0, $item->qty, $item->name, $item->desc, $item->taxid, $item->tax, $item->unit, $item->price); if ($itemid === false) { // Roll back transaction $this->deleteInvoice(); $result['error'] = "Could not insert invoice item, transaction rolled back : " . $itemMdl->errorInfo; return $result; } } } else { $this->invoice->items = []; } // insert payments if (isset($this->invoice->payments) && sizeof($this->invoice->payments) > 0) { $payMdl = new SalePaymentsModel(); foreach ($this->invoice->payments as $payment) { $payid = $payMdl->import($payment->id, $this->id, $payment->method, $payment->amount, $this->invoice->processdt); if ($payid === false) { // Roll back transaction $this->deleteInvoice(); $result['error'] = "Could not insert invoice payment, transaction rolled back : " . $payMdl->errorInfo; return $result; } } } else { $this->invoice->payments = []; } // update invoice data if ($this->saveInvoiceData() === false) { // Roll back transaction $this->deleteInvoice(); $result['error'] = "Could not commit invoice data: " . $this->invMdl->errorInfo; return $result; } $result['data'] = $this->invoice; return $result; }