db->insert('journals', $data)) { throw new RuntimeException('Gagal membuat header jurnal.'); } return (int) $this->db->insert_id(); } public function addLines(array $lines) { if (empty($lines) || !$this->db->insert_batch('journal_details', $lines)) { throw new RuntimeException('Gagal membuat detail jurnal.'); } } public function referenceExists($reference, $excludeId = 0) { $this->db->where('no_ref', $reference); if ((int) $excludeId > 0) $this->db->where('id !=', (int) $excludeId); return $this->db->count_all_results('journals') > 0; } public function sourceExists($type, $id) { return $this->db ->where('ref_type', $type) ->where('ref_id', $id) ->count_all_results('journals') > 0; } public function find($id) { return $this->db->get_where('journals', array('id' => (int) $id))->row(); } public function findForUpdate($id) { return $this->db->query('SELECT * FROM journals WHERE id = ? FOR UPDATE', array((int) $id))->row(); } public function lines($journalId) { return $this->db->where('journal_id', (int) $journalId)->order_by('id', 'ASC')->get('journal_details')->result_array(); } public function updateStatus($journalId, $status, array $extra = array()) { return $this->db->where('id', (int) $journalId)->update('journals', array_merge(array( 'status' => $status, 'updated_at' => date('Y-m-d H:i:s') ), $extra)); } public function addStatusHistory($journalId, $from, $to, $userId, $notes = null) { return $this->db->insert('journal_status_histories', array( 'journal_id' => (int) $journalId, 'from_status' => $from, 'to_status' => $to, 'notes' => $notes, 'changed_by' => (int) $userId, 'changed_at' => date('Y-m-d H:i:s') )); } }