/** * edit method * * @throws NotFoundException * @param string $id * @return void */ public function edit($id = null) { $this->set('title_for_layout', __d('webzash', 'Edit Account Ledger')); /* Check for valid ledger */ if (empty($id)) { $this->Session->setFlash(__d('webzash', 'Account ledger not specified.'), 'danger'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'accounts', 'action' => 'show')); } $ledger = $this->Ledger->findById($id); if (!$ledger) { $this->Session->setFlash(__d('webzash', 'Account ledger not found.'), 'danger'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'accounts', 'action' => 'show')); } /* Create list of parent groups */ $parentGroups = new GroupTree(); $parentGroups->Group =& $this->Group; $parentGroups->current_id = -1; $parentGroups->build(0); $parentGroups->toList($parentGroups, -1); $this->set('parents', $parentGroups->groupList); /* on POST */ if ($this->request->is('post') || $this->request->is('put')) { /* Check if acccount is locked */ if (Configure::read('Account.locked') == 1) { $this->Session->setFlash(__d('webzash', 'Sorry, no changes are possible since the account is locked.'), 'danger'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'accounts', 'action' => 'show')); } /* If code is empty set it as NULL */ if (empty($this->request->data['Ledger']['code'])) { $this->request->data['Ledger']['code'] = NULL; } /* If opening balance is not set or empty make it 0 */ if (empty($this->request->data['Ledger']['op_balance'])) { $this->request->data['Ledger']['op_balance'] = 0; } /* Count number of decimal places */ if (countDecimal($this->request->data['Ledger']['op_balance']) > Configure::read('Account.decimal_places')) { $this->Session->setFlash(__d('webzash', 'Invalid amount specified. Maximum %s decimal places allowed.', Configure::read('Account.decimal_places')), 'danger'); return; } /* Set ledger id */ unset($this->request->data['Ledger']['id']); $this->Ledger->id = $id; /* Save ledger */ $ds = $this->Ledger->getDataSource(); $ds->begin(); if ($this->Ledger->save($this->request->data)) { $this->Log->add('Edited Ledger : ' . $this->request->data['Ledger']['name'], 1); $ds->commit(); $this->Session->setFlash(__d('webzash', 'Account ledger "%s" updated.', $this->request->data['Ledger']['name']), 'success'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'accounts', 'action' => 'show')); } else { $ds->rollback(); $this->Session->setFlash(__d('webzash', 'Failed to update account ledger. Please, try again.'), 'danger'); return; } } else { $this->request->data = $ledger; return; } }
/** * edit method * * @param string $entrytypeLabel * @param string $id * @return void */ public function edit($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', 'Edit %s Entry', $entrytype['Entrytype']['name'])); $this->set('tag_options', $this->Tag->listAll()); /* Ledger selection */ $ledgers = new LedgerTree(); $ledgers->Group =& $this->Group; $ledgers->Ledger =& $this->Ledger; $ledgers->current_id = -1; $ledgers->restriction_bankcash = $entrytype['Entrytype']['restriction_bankcash']; $ledgers->build(0); $ledgers->toList($ledgers, -1); $ledgers_disabled = array(); foreach ($ledgers->ledgerList as $row => $data) { if ($row < 0) { $ledgers_disabled[] = $row; } } $this->set('ledger_options', $ledgers->ledgerList); $this->set('ledgers_disabled', $ledgers_disabled); /* 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')); } /* Initial data */ if ($this->request->is('post') || $this->request->is('put')) { $curEntryitems = array(); foreach ($this->request->data['Entryitem'] as $row => $entryitem) { $curEntryitems[$row] = array('dc' => $entryitem['dc'], 'ledger_id' => $entryitem['ledger_id'], 'dr_amount' => isset($entryitem['dr_amount']) ? $entryitem['dr_amount'] : '', 'cr_amount' => isset($entryitem['cr_amount']) ? $entryitem['cr_amount'] : ''); } $this->set('curEntryitems', $curEntryitems); } else { $curEntryitems = array(); $curEntryitemsData = $this->Entryitem->find('all', array('conditions' => array('Entryitem.entry_id' => $id))); foreach ($curEntryitemsData as $row => $data) { if ($data['Entryitem']['dc'] == 'D') { $curEntryitems[$row] = array('dc' => $data['Entryitem']['dc'], 'ledger_id' => $data['Entryitem']['ledger_id'], 'dr_amount' => $data['Entryitem']['amount'], 'cr_amount' => ''); } else { $curEntryitems[$row] = array('dc' => $data['Entryitem']['dc'], 'ledger_id' => $data['Entryitem']['ledger_id'], 'dr_amount' => '', 'cr_amount' => $data['Entryitem']['amount']); } } $curEntryitems[] = array('dc' => 'D'); $curEntryitems[] = array('dc' => 'D'); $curEntryitems[] = array('dc' => 'D'); $this->set('curEntryitems', $curEntryitems); } /* On POST */ if ($this->request->is('post') || $this->request->is('put')) { if (!empty($this->request->data)) { /* Check if acccount is locked */ if (Configure::read('Account.locked') == 1) { $this->Session->setFlash(__d('webzash', 'Sorry, no changes are possible since the account is locked.'), 'danger'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index')); } /***************************************************************************/ /*********************************** ENTRY *********************************/ /***************************************************************************/ $entrydata = null; /* Entry id */ unset($this->request->data['Entry']['id']); $this->Entry->id = $id; $entrydata['Entry']['id'] = $id; /***** Entry number ******/ $entrydata['Entry']['number'] = $this->request->data['Entry']['number']; /****** Entrytype remains the same *****/ $entrydata['Entry']['entrytype_id'] = $entrytype['Entrytype']['id']; /****** Check tag ******/ if (empty($this->request->data['Entry']['tag_id'])) { $entrydata['Entry']['tag_id'] = null; } else { $entrydata['Entry']['tag_id'] = $this->request->data['Entry']['tag_id']; } /***** Narration *****/ $entrydata['Entry']['narration'] = $this->request->data['Entry']['narration']; /***** Date *****/ $entrydata['Entry']['date'] = dateToSql($this->request->data['Entry']['date']); /***************************************************************************/ /***************************** ENTRY ITEMS *********************************/ /***************************************************************************/ /* Check ledger restriction */ $dc_valid = false; foreach ($this->request->data['Entryitem'] as $row => $entryitem) { if ($entryitem['ledger_id'] <= 0) { continue; } $ledger = $this->Ledger->findById($entryitem['ledger_id']); if (!$ledger) { $this->Session->setFlash(__d('webzash', 'Invalid ledger selected.'), 'danger'); return; } if ($entrytype['Entrytype']['restriction_bankcash'] == 4) { if ($ledger['Ledger']['type'] != 1) { $this->Session->setFlash(__d('webzash', 'Only bank or cash ledgers are allowed for this entry type.'), 'danger'); return; } } if ($entrytype['Entrytype']['restriction_bankcash'] == 5) { if ($ledger['Ledger']['type'] == 1) { $this->Session->setFlash(__d('webzash', 'Bank or cash ledgers are not allowed for this entry type.'), 'danger'); return; } } if ($entryitem['dc'] == 'D') { if ($entrytype['Entrytype']['restriction_bankcash'] == 2) { if ($ledger['Ledger']['type'] == 1) { $dc_valid = true; } } } else { if ($entryitem['dc'] == 'C') { if ($entrytype['Entrytype']['restriction_bankcash'] == 3) { if ($ledger['Ledger']['type'] == 1) { $dc_valid = true; } } } } } if ($entrytype['Entrytype']['restriction_bankcash'] == 2) { if (!$dc_valid) { $this->Session->setFlash(__d('webzash', 'Atleast one bank or cash ledger has to be on debit side for this entry type.'), 'danger'); return; } } if ($entrytype['Entrytype']['restriction_bankcash'] == 3) { if (!$dc_valid) { $this->Session->setFlash(__d('webzash', 'Atleast one bank or cash ledger has to be on credit side for this entry type.'), 'danger'); return; } } $dr_total = 0; $cr_total = 0; /* Check equality of debit and credit total */ foreach ($this->request->data['Entryitem'] as $row => $entryitem) { if ($entryitem['ledger_id'] <= 0) { continue; } if ($entryitem['dc'] == 'D') { if ($entryitem['dr_amount'] <= 0) { $this->Session->setFlash(__d('webzash', 'Invalid amount specified. Amount cannot be negative or zero.'), 'danger'); return; } if (countDecimal($entryitem['dr_amount']) > Configure::read('Account.decimal_places')) { $this->Session->setFlash(__d('webzash', 'Invalid amount specified. Maximum %s decimal places allowed.', Configure::read('Account.decimal_places')), 'danger'); return; } $dr_total = calculate($dr_total, $entryitem['dr_amount'], '+'); } else { if ($entryitem['dc'] == 'C') { if ($entryitem['cr_amount'] <= 0) { $this->Session->setFlash(__d('webzash', 'Invalid amount specified. Amount cannot be negative or zero.'), 'danger'); return; } if (countDecimal($entryitem['cr_amount']) > Configure::read('Account.decimal_places')) { $this->Session->setFlash(__d('webzash', 'Invalid amount specified. Maximum %s decimal places allowed.', Configure::read('Account.decimal_places')), 'danger'); return; } $cr_total = calculate($cr_total, $entryitem['cr_amount'], '+'); } else { $this->Session->setFlash(__d('webzash', 'Invalid Dr/Cr option selected.'), 'danger'); return; } } } if (calculate($dr_total, $cr_total, '!=')) { $this->Session->setFlash(__d('webzash', 'Debit and Credit total do not match.'), 'danger'); return; } $entrydata['Entry']['dr_total'] = $dr_total; $entrydata['Entry']['cr_total'] = $cr_total; /* Add item to entryitemdata array if everything is ok */ $entryitemdata = array(); foreach ($this->request->data['Entryitem'] as $row => $entryitem) { if ($entryitem['ledger_id'] <= 0) { continue; } if ($entryitem['dc'] == 'D') { $entryitemdata[] = array('Entryitem' => array('dc' => $entryitem['dc'], 'ledger_id' => $entryitem['ledger_id'], 'amount' => $entryitem['dr_amount'])); } else { $entryitemdata[] = array('Entryitem' => array('dc' => $entryitem['dc'], 'ledger_id' => $entryitem['ledger_id'], 'amount' => $entryitem['cr_amount'])); } } /* Save entry */ $ds = $this->Entry->getDataSource(); $ds->begin(); if ($this->Entry->save($entrydata)) { /* Delete all original entryitems */ if (!$this->Entryitem->deleteAll(array('Entryitem.entry_id' => $id))) { $ds->rollback(); $this->Session->setFlash(__d('webzash', 'Previous entry items could not be deleted.'), 'danger'); return; } /* Save new entry items */ foreach ($entryitemdata as $id => $itemdata) { $itemdata['Entryitem']['entry_id'] = $this->Entry->id; $this->Entryitem->create(); if (!$this->Entryitem->save($itemdata)) { 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; } } $tempentry = $this->Entry->read(null, $this->Entry->id); if (!$tempentry) { $this->Session->setFlash(__d('webzash', 'Oh snap ! Failed to update entry. Please, try again.'), 'danger'); $ds->rollback(); return; } $entryNumber = h(toEntryNumber($tempentry['Entry']['number'], $entrytype['Entrytype']['id'])); $this->Log->add('Edited ' . $entrytype['Entrytype']['name'] . ' entry numbered ' . $entryNumber, 1); $ds->commit(); $this->Session->setFlash(__d('webzash', '%s entry numbered "%s" updated.', $entrytype['Entrytype']['name'], $entryNumber), 'success'); return $this->redirect(array('plugin' => 'webzash', 'controller' => 'entries', 'action' => 'index')); } else { $ds->rollback(); $this->Session->setFlash(__d('webzash', 'Failed to update entry. Please, try again.'), 'danger'); return; } } else { $this->Session->setFlash(__d('webzash', 'No data. Please, try again.'), 'danger'); return; } } else { $entry['Entry']['date'] = dateFromSql($entry['Entry']['date']); $this->request->data = $entry; return; } }