Skip to content

Commit

Permalink
Merge pull request #6592 from aspangaro/5.0-p5
Browse files Browse the repository at this point in the history
Fix : integration of expense report in accountancy bank journal
  • Loading branch information
eldy committed Mar 28, 2017
2 parents 09e1583 + 3148841 commit ede23aa
Showing 1 changed file with 77 additions and 13 deletions.
90 changes: 77 additions & 13 deletions htdocs/accountancy/journal/bankjournal.php
Expand Up @@ -4,7 +4,7 @@
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
* Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
* Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
* Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
*
Expand Down Expand Up @@ -48,6 +48,9 @@
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php';
require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php';
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php';


// Langs
$langs->load("companies");
Expand All @@ -57,6 +60,8 @@
$langs->load('bills');
$langs->load('donations');
$langs->load("accountancy");
$langs->load("trips");
$langs->load("hrm");

$id_bank_account = GETPOST('id_account', 'int');

Expand Down Expand Up @@ -103,11 +108,14 @@

$sql = "SELECT b.rowid , b.dateo as do, b.datev as dv, b.amount, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type,";
$sql .= " ba.courant, ba.ref as baref, ba.account_number,";
$sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop";
$sql .= " soc.code_compta, soc.code_compta_fournisseur, soc.rowid as socid, soc.nom as name, bu1.type as typeop,";
$sql .= " u.accountancy_code, u.rowid as userid, u.lastname as name, u.firstname as firstname, bu2.type as typeop";
$sql .= " FROM " . MAIN_DB_PREFIX . "bank as b";
$sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as soc on bu1.url_id=soc.rowid";
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u on bu2.url_id=u.rowid";
$sql .= " WHERE ba.rowid=" . $id_bank_account;
$sql .= ' AND ba.entity IN ('.getEntity('bank_account', 0).')'; // We don't share object for accountancy
if ($date_start && $date_end)
Expand All @@ -123,6 +131,8 @@
$paymentdonstatic = new PaymentDonation($db);
$paymentvatstatic = new TVA($db);
$paymentsalstatic = new PaymentSalary($db);
$paymentsalstatic = new PaymentSalary($db);
$paymentexpensereportstatic = new PaymentExpenseReport($db);

// Get code of finance journal
$bank_code_journal = new Account($db);
Expand All @@ -143,6 +153,7 @@
$account_transfer = (! empty($conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH) ? $conf->global->ACCOUNTING_ACCOUNT_TRANSFER_CASH : $langs->trans("CodeNotDef"));

$tabcompany = array();
$tabuser = array();
$tabpay = array ();
$tabbq = array ();
$tabtp = array ();
Expand All @@ -169,6 +180,15 @@
'name' => $obj->name,
'code_compta' => $compta_soc,
);

$compta_user = (! empty($obj->accountancy_code) ? $obj->accountancy_code : $account_employee);

$tabuser[$obj->rowid] = array (
'id' => $obj->userid,
'lastname' => $obj->lastname,
'firstname' => $obj->firstname,
'accountancy_code' => $compta_user,
);

// Variable bookkeeping
$tabpay[$obj->rowid]["date"] = $obj->do;
Expand All @@ -187,7 +207,7 @@
// Now loop on each link of record in bank.
foreach ( $links as $key => $val ) {

if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat'))) // So we excluded 'company' here
if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport'))) // So we excluded 'company' here
{
// We save tabtype for a future use, to remember what kind of payment it is
$tabtype[$obj->rowid] = $links[$key]['type'];
Expand All @@ -211,7 +231,7 @@
$userstatic->id = $links[$key]['url_id'];
$userstatic->name = $links[$key]['label'];
$tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, '', 30);
// $tabtp[$obj->rowid][$compta_user] += $obj->amount;
$tabtp[$obj->rowid][$compta_user] += $obj->amount;
} else if ($links[$key]['type'] == 'sc') {
$chargestatic->id = $links[$key]['url_id'];
$chargestatic->ref = $links[$key]['url_id'];
Expand Down Expand Up @@ -256,7 +276,12 @@
$paymentsalstatic->ref = $links[$key]['url_id'];
$paymentsalstatic->label = $links[$key]['label'];
$tabpay[$obj->rowid]["lib"] .= ' ' . $paymentsalstatic->getNomUrl(2);
$tabtp[$obj->rowid][$account_employee] += $obj->amount;
// $tabtp[$obj->rowid][$account_employee] += $obj->amount;
} else if ($links[$key]['type'] == 'payment_expensereport') {
$paymentexpensereportstatic->id = $links[$key]['url_id'];
$paymentexpensereportstatic->fk_expensereport = $links[$key]['url_id'];
$tabpay[$obj->rowid]["lib"] .= ' ' . $paymentexpensereportstatic->getNomUrl(2);
$tabpay[$obj->rowid]["fk_expensereport"] = $paymentexpensereportstatic->id;
} else if ($links[$key]['type'] == 'banktransfert') {
$tabpay[$obj->rowid]["lib"] .= ' ' . $langs->trans("BankTransfer");
$tabtp[$obj->rowid][$account_transfer] += $obj->amount;
Expand Down Expand Up @@ -335,10 +360,10 @@

if ($tabtype[$key] == 'payment') {
$bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];

$sqlmid = 'SELECT fac.facnumber';
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac ";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture fac";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement_facture as payfac ON payfac.fk_facture=fac.rowid";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiement as pay ON payfac.fk_paiement=pay.rowid";
$sqlmid .= " WHERE pay.fk_bank=" . $key;
dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
Expand All @@ -349,10 +374,10 @@
}
} else if ($tabtype[$key] == 'payment_supplier') {
$bookkeeping->code_tiers = $tabcompany[$key]['code_compta'];

$sqlmid = 'SELECT facf.ref_supplier, facf.ref';
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf ";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "facture_fourn facf";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn_facturefourn as payfacf ON payfacf.fk_facturefourn=facf.rowid";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "paiementfourn as payf ON payfacf.fk_paiementfourn=payf.rowid";
$sqlmid .= " WHERE payf.fk_bank=" . $key;
dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
Expand All @@ -361,6 +386,19 @@
$objmid = $db->fetch_object($resultmid);
$bookkeeping->doc_ref = $objmid->ref_supplier . ' (' . $objmid->ref . ')'; // Ref on invoice
}
} else if ($tabtype[$key] == 'payment_expensereport') {
$bookkeeping->code_tiers = $tabuser[$key]['accountancy_code'];

$sqlmid = 'SELECT e.ref';
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "expensereport as e";
$sqlmid .= " INNER JOIN " . MAIN_DB_PREFIX . "payment_expensereport as payer ON payer.fk_expensereport=e.rowid";
$sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=" . $sqlmid, LOG_DEBUG);
$resultmid = $db->query($sqlmid);
if ($resultmid) {
$objmid = $db->fetch_object($resultmid);
$bookkeeping->doc_ref = $objmid->ref; // Ref of expensereport
}
}

$result = $bookkeeping->create($user);
Expand Down Expand Up @@ -481,6 +519,7 @@
include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';

$companystatic = new Client($db);
$userstatic = new User($db);

// Model Cegid Expert Export
if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2)
Expand All @@ -504,12 +543,19 @@
$reflabel = $langs->trans('Donation');
}
if ($reflabel == '(SubscriptionPayment)') {
$reflabel = $langs->trans('Donation');
$reflabel = $langs->trans('Subscription');
}
if ($reflabel == '(ExpenseReportPayment)') {
$reflabel = $langs->trans('Employee');
}

$companystatic->id = $tabcompany[$key]['id'];
$companystatic->name = $tabcompany[$key]['name'];

$userstatic->id = $tabuser[$key]['id'];
$userstatic->lastname = $tabuser[$key]['lastname'];
$userstatic->firstname = $tabuser[$key]['firstname'];

// Bank
foreach ( $tabbq[$key] as $k => $mt ) {
print $date . $sep;
Expand Down Expand Up @@ -646,6 +692,7 @@
if (empty($action) || $action == 'view') {
$invoicestatic = new Facture($db);
$invoicesupplierstatic = new FactureFournisseur($db);
$expensereportstatic = new ExpenseReport($db);

llxHeader('', $langs->trans("FinanceJournal"));

Expand Down Expand Up @@ -723,7 +770,10 @@ function writebookkeeping() {
$reflabel = $langs->trans('Donation');
}
if ($reflabel == '(SubscriptionPayment)') {
$reflabel = $langs->trans('SubscriptionPayment');
$reflabel = $langs->trans('Subscription');
}
if ($reflabel == '(ExpenseReportPayment)') {
$reflabel = $langs->trans('Employee');
}

$ref=$reflabel;
Expand Down Expand Up @@ -755,6 +805,20 @@ function writebookkeeping() {
}
else dol_print_error($db);
}
elseif ($tabtype[$key] == 'payment_expensereport')
{
$sqlmid = 'SELECT payer.fk_expensereport as id';
$sqlmid .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as payer";
$sqlmid .= " WHERE payer.fk_expensereport=" . $val["fk_expensereport"];
dol_syslog("accountancy/journal/bankjournal.php::sqlmid=" . $sqlmid, LOG_DEBUG);
$resultmid = $db->query($sqlmid);
if ($resultmid) {
$objmid = $db->fetch_object($resultmid);
$expensereportstatic->fetch($objmid->id);
$ref=$langs->trans("ExpenseReport").' '.$expensereportstatic->getNomUrl(1);
}
else dol_print_error($db);
}


/*$invoicestatic->id = $key;
Expand Down

0 comments on commit ede23aa

Please sign in to comment.