* Copyright (C) 2015 Frederic France * Copyright (C) 2017 Laurent Destailleur * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/loan/card.php * \ingroup loan * \brief Loan card */ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; $langs->load("compta"); $langs->load("bills"); $langs->load("loan"); $id=GETPOST('id','int'); $action=GETPOST('action','aZ09'); $confirm=GETPOST('confirm'); $cancel=GETPOST('cancel','alpha'); $projectid = GETPOST('projectid','int'); // Security check $socid = GETPOST('socid','int'); if ($user->societe_id) $socid=$user->societe_id; $result = restrictedArea($user, 'loan', $id, '',''); $object = new Loan($db); $hookmanager->initHooks(array('loancard','globalcard')); /* * Actions */ $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); if (empty($reshook)) { // Classify paid if ($action == 'confirm_paid' && $confirm == 'yes') { $object->fetch($id); $result = $object->set_paid($user); if ($result > 0) { setEventMessages($langs->trans('LoanPaid'), null, 'mesgs'); } else { setEventMessages($loan->error, null, 'errors'); } } // Delete loan if ($action == 'confirm_delete' && $confirm == 'yes') { $object->fetch($id); $result=$object->delete($user); if ($result > 0) { setEventMessages($langs->trans('LoanDeleted'), null, 'mesgs'); header("Location: index.php"); exit; } else { setEventMessages($loan->error, null, 'errors'); } } // Add loan if ($action == 'add' && $user->rights->loan->write) { if (! $cancel) { $datestart = dol_mktime(12, 0, 0, GETPOST('startmonth','int'), GETPOST('startday','int'), GETPOST('startyear','int')); $dateend = dol_mktime(12, 0, 0, GETPOST('endmonth','int'), GETPOST('endday','int'), GETPOST('endyear','int')); $capital = price2num(GETPOST('capital')); $rate = GETPOST('rate'); if (! $capital) { $error++; $action = 'create'; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("LoanCapital")), null, 'errors'); } if (! $datestart) { $error++; $action = 'create'; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("DateStart")), null, 'errors'); } if (! $dateend) { $error++; $action = 'create'; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("DateEnd")), null, 'errors'); } if ($rate == '') { $error++; $action = 'create'; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Rate")), null, 'errors'); } if (! $error) { $object->label = GETPOST('label'); $object->fk_bank = GETPOST('accountid'); $object->capital = $capital; $object->datestart = $datestart; $object->dateend = $dateend; $object->nbterm = GETPOST('nbterm'); $object->rate = $rate; $object->note_private = GETPOST('note_private'); $object->note_public = GETPOST('note_public'); $object->fk_project = GETPOST('projectid','int'); $accountancy_account_capital = GETPOST('accountancy_account_capital'); $accountancy_account_insurance = GETPOST('accountancy_account_insurance'); $accountancy_account_interest = GETPOST('accountancy_account_interest'); if ($accountancy_account_capital <= 0) { $object->account_capital = ''; } else { $object->account_capital = $accountancy_account_capital; } if ($accountancy_account_insurance <= 0) { $object->account_insurance = ''; } else { $object->account_insurance = $accountancy_account_insurance; } if ($accountancy_account_interest <= 0) { $object->account_interest = ''; } else { $object->account_interest = $accountancy_account_interest; } $id=$object->create($user); if ($id <= 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); $action = 'create'; } } } else { header("Location: index.php"); exit(); } } // Update record else if ($action == 'update' && $user->rights->loan->write) { if (! $cancel) { $result = $object->fetch($id); $datestart = dol_mktime(12, 0, 0, GETPOST('startmonth','int'), GETPOST('startday','int'), GETPOST('startyear','int')); $dateend = dol_mktime(12, 0, 0, GETPOST('endmonth','int'), GETPOST('endday','int'), GETPOST('endyear','int')); $capital = price2num(GETPOST('capital')); if (! $capital) { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("LoanCapital")), null, 'errors'); $action = 'edit'; } else { $object->datestart = $datestart; $object->dateend = $dateend; $object->capital = $capital; $object->nbterm = GETPOST("nbterm"); $object->rate = GETPOST("rate"); $accountancy_account_capital = GETPOST('accountancy_account_capital'); $accountancy_account_insurance = GETPOST('accountancy_account_insurance'); $accountancy_account_interest = GETPOST('accountancy_account_interest'); if ($accountancy_account_capital <= 0) { $object->account_capital = ''; } else { $object->account_capital = $accountancy_account_capital; } if ($accountancy_account_insurance <= 0) { $object->account_insurance = ''; } else { $object->account_insurance = $accountancy_account_insurance; } if ($accountancy_account_interest <= 0) { $object->account_interest = ''; } else { $object->account_interest = $accountancy_account_interest; } } $result = $object->update($user); if ($result > 0) { header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); } } else { header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id); exit; } } // Link to a project if ($action == 'classin' && $user->rights->loan->write) { $object->fetch($id); $result = $object->setProject($projectid); if ($result < 0) setEventMessages($object->error, $object->errors, 'errors'); } if ($action == 'setlabel' && $user->rights->loan->write) { $object->fetch($id); $result = $object->setValueFrom('label', GETPOST('label'), '', '', 'text', '', $user, 'LOAN_MODIFY'); if ($result < 0) setEventMessages($object->error, $object->errors, 'errors'); } } /* * View */ $form = new Form($db); $formproject = new FormProjets($db); if (! empty($conf->accounting->enabled)) $formaccounting = New FormAccounting($db); $title = $langs->trans("Loan") . ' - ' . $langs->trans("Card"); $help_url = 'EN:Module_Loan|FR:Module_Emprunt'; llxHeader("",$title,$help_url); // Create mode if ($action == 'create') { //WYSIWYG Editor require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; print load_fiche_titre($langs->trans("NewLoan"), '', 'title_accountancy.png'); $datec = dol_mktime(12, 0, 0, GETPOST('remonth','int'), GETPOST('reday','int'), GETPOST('reyear','int')); print '
' . "\n"; print ''; print ''; dol_fiche_head(); print ''; // Label print ''; // Bank account if (! empty($conf->banque->enabled)) { print ''; } else { print ''; } // Capital print ''; // Date Start print ""; print ''; // Date End print ""; print ''; // Number of terms print ''; // Rate print ''; // Project if (! empty($conf->projet->enabled)) { $formproject=new FormProjets($db); // Projet associe $langs->load("projects"); print ''; } // Note Private print ''; print ''; print ''; // Note Public print ''; print ''; print ''; // Accountancy if (! empty($conf->accounting->enabled)) { // Accountancy_account_capital print ''; print ''; // Accountancy_account_insurance print ''; print ''; // Accountancy_account_interest print ''; print ''; } else // For external software { // Accountancy_account_capital print ''; print ''; // Accountancy_account_insurance print ''; print ''; // Accountancy_account_interest print ''; print ''; } print '
'.$langs->trans("Label").'
'.$langs->trans("Account").''; $form->select_comptes(GETPOST("accountid"),"accountid",0,"courant=1",1); // Show list of bank account with courant print '
'.$langs->trans("Account").''; print $langs->trans("NoBankAccountDefined"); print '
'.$langs->trans("LoanCapital").'
'.$langs->trans("DateStart").''; print $form->select_date($datestart?$datestart:-1,'start','','','','add',1,1,1); print '
'.$langs->trans("DateEnd").''; print $form->select_date($dateend?$dateend:-1,'end','','','','add',1,1,1); print '
'.$langs->trans("Nbterms").'
'.$langs->trans("Rate").' %
'.$langs->trans("Project").''; $numproject=$formproject->select_projects(-1, $projectid, 'projectid', 16, 0, 1, 1); print '
'.$langs->trans('NotePrivate').''; $doleditor = new DolEditor('note_private', GETPOST('note_private', 'alpha'), '', 160, 'dolibarr_notes', 'In', false, true, true, ROWS_6, '90%'); print $doleditor->Create(1); print '
'.$langs->trans('NotePublic').''; $doleditor = new DolEditor('note_public', GETPOST('note_public', 'alpha'), '', 160, 'dolibarr_notes', 'In', false, true, true, ROWS_6, '90%'); print $doleditor->Create(1); print '
'.$langs->trans("LoanAccountancyCapitalCode").''; print $formaccounting->select_account($object->accountancy_account_capital, 'accountancy_account_capital', 1, '', 0, 1); print '
'.$langs->trans("LoanAccountancyInsuranceCode").''; print $formaccounting->select_account($object->accountancy_account_insurance, 'accountancy_account_insurance', 1, '', 0, 1); print '
'.$langs->trans("LoanAccountancyInterestCode").''; print $formaccounting->select_account($object->accountancy_account_interest, 'accountancy_account_interest', 1, '', 0, 1); print '
'.$langs->trans("LoanAccountancyCapitalCode").''; print '
'.$langs->trans("LoanAccountancyInsuranceCode").''; print '
'.$langs->trans("LoanAccountancyInterestCode").''; print '
'; dol_fiche_end(); print '
'; print ''; print '     '; print ''; print '
'; print '
'; } // View if ($id > 0) { $object = new Loan($db); $result = $object->fetch($id); if ($result > 0) { $head=loan_prepare_head($object); $totalpaid = $object->getSumPayment(); // Confirm for loan if ($action == 'paid') { $text=$langs->trans('ConfirmPayLoan'); print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id,$langs->trans('PayLoan'),$text,"confirm_paid",'','',2); } if ($action == 'delete') { $text=$langs->trans('ConfirmDeleteLoan'); print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id,$langs->trans('DeleteLoan'),$text,'confirm_delete','','',2); } if ($action == 'edit') { print '
' . "\n"; print ''; print ''; print ''; } dol_fiche_head($head, 'card', $langs->trans("Loan"), -1, 'bill'); print ''; // Loan card $linkback = '' . $langs->trans("BackToList") . ''; $morehtmlref='
'; // Ref loan $morehtmlref.=$form->editfieldkey("Label", 'label', $object->label, $object, $user->rights->loan->write, 'string', '', 0, 1); $morehtmlref.=$form->editfieldval("Label", 'label', $object->label, $object, $user->rights->loan->write, 'string', '', null, null, '', 1); // Project if (! empty($conf->projet->enabled)) { $langs->load("projects"); $morehtmlref.='
'.$langs->trans('Project') . ' '; if ($user->rights->loan->write) { if ($action != 'classify') $morehtmlref.='' . img_edit($langs->transnoentitiesnoconv('SetProject')) . ' : '; if ($action == 'classify') { //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1); $morehtmlref.=''; $morehtmlref.=''; $morehtmlref.=''; $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1); $morehtmlref.=''; $morehtmlref.=''; } else { $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1); } } else { if (! empty($object->fk_project)) { $proj = new Project($db); $proj->fetch($object->fk_project); $morehtmlref.=''; $morehtmlref.=$proj->ref; $morehtmlref.=''; } else { $morehtmlref.=''; } } } $morehtmlref.='
'; $object->totalpaid = $totalpaid; // To give a chance to dol_banner_tab to use already paid amount to show correct status dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright); print '
'; print '
'; print '
'; print ''; // Capital if ($action == 'edit') { print ''; print ''; } else { print ''; } // Date start print '"; print ""; // Date end print '"; print ""; // Nbterms print ''; print ''; // Rate print ''; print ''; // Accountancy account capital print ''; // Accountancy account insurance print ''; // Accountancy account interest print ''; print '
'.$langs->trans("LoanCapital").''; print '
'.$langs->trans("LoanCapital").''.price($object->capital,0,$outputlangs,1,-1,-1,$conf->currency).'
'.$langs->trans("DateStart").""; if ($action == 'edit') { print $form->select_date($object->datestart, 'start', 0, 0, 0, 'update', 1, 0, 1); } else { print dol_print_date($object->datestart,"day"); } print "
'.$langs->trans("DateEnd").""; if ($action == 'edit') { print $form->select_date($object->dateend, 'end', 0, 0, 0, 'update', 1, 0, 1); } else { print dol_print_date($object->dateend,"day"); } print "
'.$langs->trans("Nbterms").''; if ($action == 'edit') { print ''; } else { print $object->nbterm; } print '
'.$langs->trans("Rate").''; if ($action == 'edit') { print '%'; } else { print $object->rate . '%'; } print '
'; print $langs->trans("LoanAccountancyCapitalCode"); print ''; if ($action == 'edit') { if (! empty($conf->accounting->enabled)) { print $formaccounting->select_account($object->account_capital, 'accountancy_account_capital', 1, '', 0, 1); } else { print ''; } } else { if (! empty($conf->accounting->enabled)) { print length_accountg($object->account_capital); } else { print $object->account_capital; } } print '
'; print $langs->trans("LoanAccountancyInsuranceCode"); print ''; if ($action == 'edit') { if (! empty($conf->accounting->enabled)) { print $formaccounting->select_account($object->account_insurance, 'accountancy_account_insurance', 1, '', 0, 1); } else { print ''; } } else { if (! empty($conf->accounting->enabled)) { print length_accountg($object->account_insurance); } else { print $object->account_insurance; } } print '
'; print $langs->trans("LoanAccountancyInterestCode"); print ''; if ($action == 'edit') { if (! empty($conf->accounting->enabled)) { print $formaccounting->select_account($object->account_interest, 'accountancy_account_interest', 1, '', 0, 1); } else { print ''; } } else { if (! empty($conf->accounting->enabled)) { print length_accountg($object->account_interest); } else { print $object->account_interest; } } print '
'; print '
'; print '
'; print '
'; /* * Payments */ $sql = "SELECT p.rowid, p.num_payment, datep as dp,"; $sql.= " p.amount_capital, p.amount_insurance, p.amount_interest,"; $sql.= " c.libelle as paiement_type"; $sql.= " FROM ".MAIN_DB_PREFIX."payment_loan as p"; $sql.= ", ".MAIN_DB_PREFIX."c_paiement as c "; $sql.= ", ".MAIN_DB_PREFIX."loan as l"; $sql.= " WHERE p.fk_loan = ".$id; $sql.= " AND p.fk_loan = l.rowid"; $sql.= " AND l.entity = ".$conf->entity; $sql.= " AND p.fk_typepayment = c.id"; $sql.= " ORDER BY dp DESC"; //print $sql; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; $total_insurance = 0; $total_interest = 0; $total_capital = 0; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $var=True; while ($i < $num) { $objp = $db->fetch_object($resql); print ''; print ''; print '\n"; print "\n"; print '\n"; print '\n"; print '\n"; print ""; $total_capital += $objp->amount_capital; $i++; } $totalpaid = $total_capital; if ($object->paid == 0) { print ''; print ''; $staytopay = $object->capital - $totalpaid; print ''; print ''; } print "
'.$langs->trans("RefPayment").''.$langs->trans("Date").''.$langs->trans("Type").''.$langs->trans("Insurance").''.$langs->trans("Interest").''.$langs->trans("LoanCapital").'
'.img_object($langs->trans("Payment"),"payment").' '.$objp->rowid.''.dol_print_date($db->jdate($objp->dp),'day')."".$objp->paiement_type.' '.$objp->num_payment."'.price($objp->amount_insurance, 0, $langs, 0, 0, -1, $conf->currency)."'.price($objp->amount_interest, 0, $langs, 0, 0, -1, $conf->currency)."'.price($objp->amount_capital, 0, $langs, 0, 0, -1, $conf->currency)."
'.$langs->trans("AlreadyPaid").' :'.price($totalpaid, 0, $langs, 0, 0, -1, $conf->currency).'
'.$langs->trans("AmountExpected").' :'.price($object->capital,0,$outputlangs,1,-1,-1,$conf->currency).'
'.$langs->trans("RemainderToPay").' :'.price($staytopay, 0, $langs, 0, 0, -1, $conf->currency).'
"; $db->free($resql); } else { dol_print_error($db); } print '
'; print '
'; print '
'; print '
'; dol_fiche_end(); if ($action == 'edit') { print '
'; print ''; print '     '; print ''; print '
'; print ''; } /* * Buttons actions */ if ($action != 'edit') { $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook if (empty($reshook)) { print '
'; // Edit if ($user->rights->loan->write) { print ''.$langs->trans('CreateCalcSchedule').''; print ''.$langs->trans("Modify").''; } // Emit payment if ($object->paid == 0 && ((price2num($object->capital) > 0 && round($staytopay) < 0) || (price2num($object->capital) > 0 && round($staytopay) > 0)) && $user->rights->loan->write) { print ''.$langs->trans("DoPayment").''; } // Classify 'paid' if ($object->paid == 0 && round($staytopay) <=0 && $user->rights->loan->write) { print ''.$langs->trans("ClassifyPaid").''; } // Delete if ($user->rights->loan->delete) { print ''.$langs->trans("Delete").''; } print "
"; } } } else { // Loan not found dol_print_error('',$object->error); } } llxFooter(); $db->close();