Skip to content

Instantly share code, notes, and snippets.

@prashants
Created November 28, 2016 06:06
Show Gist options
  • Save prashants/5f2cce6979dcec4684bb4f363a11926c to your computer and use it in GitHub Desktop.
Save prashants/5f2cce6979dcec4684bb4f363a11926c to your computer and use it in GitHub Desktop.
Clone entry method
public function eclone($entrytypeLabel = null, $id = null) {
/* Check for valid entry type */
if (!$entrytypeLabel) {
$this->Session->setFlash(__d('webzash', 'Entry type not specified.'), 'danger');
return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index'));
}
$entrytype = $this->Entrytype->find('first', array('conditions' => array('Entrytype.label' => $entrytypeLabel)));
if (!$entrytype) {
$this->Session->setFlash(__d('webzash', 'Entry type not found.'), 'danger');
return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index'));
}
$this->set('entrytype', $entrytype);
$this->set('title_for_layout', __d('webzash', 'View %s Entry', $entrytype['Entrytype']['name']));
/* Check for valid entry id */
if (empty($id)) {
$this->Session->setFlash(__d('webzash', 'Entry not specified.'), 'danger');
return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index'));
}
$entry = $this->Entry->findById($id);
if (!$entry) {
$this->Session->setFlash(__d('webzash', 'Entry not found.'), 'danger');
return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index'));
}
/* Fetch entry items */
$entryitems = $this->Entryitem->find('all', array(
'conditions' => array('Entryitem.entry_id' => $id),
));
/* Create clone entry */
$clone_entry['Entry']['tag_id'] = $entry['Entry']['tag_id'];
$clone_entry['Entry']['entrytype_id'] = $entry['Entry']['entrytype_id'];
$clone_entry['Entry']['number'] = null;
$clone_entry['Entry']['date'] = $entry['Entry']['date'];
$clone_entry['Entry']['dr_total'] = $entry['Entry']['dr_total'];
$clone_entry['Entry']['cr_total'] = $entry['Entry']['cr_total'];
$clone_entry['Entry']['narration'] = $entry['Entry']['narration'];
/* Save entry */
$ds = $this->Entry->getDataSource();
$ds->begin();
$this->Entry->create();
if ($this->Entry->save($clone_entry)) {
/* Save entry items */
foreach ($entryitems as $row => $data) {
$entryitem['Entryitem'] = array(
'entry_id' => $this->Entry->id,
'ledger_id' => $data['Entryitem']['ledger_id'],
'amount' => $data['Entryitem']['amount'],
'dc' => $data['Entryitem']['dc'],
);
$this->Entryitem->create();
if (!$this->Entryitem->save($entryitem)) {
foreach ($this->Entryitem->validationErrors as $field => $msg) {
$errmsg = $msg[0];
break;
}
$ds->rollback();
$this->Session->setFlash(__d('webzash', 'Failed to save entry ledgers. Error is : %s', $errmsg), 'danger');
return;
}
}
} else {
$ds->rollback();
$this->Session->setFlash(__d('webzash', 'Failed to clone entry. Please, try again.'), 'danger');
return;
}
$ds->commit();
$this->Session->setFlash(__d('webzash', '%s entry cloned. Please updated entry number and save it.', $entrytype['Entrytype']['name']), 'success');
return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'edit', $entrytypeLabel, $this->Entry->id));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment