| Total Complexity | 658 |
| Total Lines | 5216 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AccountingJournalController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AccountingJournalController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 98 | class AccountingJournalController extends DolibarrController |
||
|
|
|||
| 99 | { |
||
| 100 | |||
| 101 | public function index() |
||
| 102 | { |
||
| 103 | return $this->bankjournal(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * \file htdocs/accountancy/journal/bankjournal.php |
||
| 108 | * \ingroup Accountancy (Double entries) |
||
| 109 | * \brief Page with bank journal |
||
| 110 | */ |
||
| 111 | public function bankjournal() |
||
| 112 | { |
||
| 113 | global $conf; |
||
| 114 | global $db; |
||
| 115 | global $user; |
||
| 116 | global $hookmanager; |
||
| 117 | global $user; |
||
| 118 | global $menumanager; |
||
| 119 | global $langs; |
||
| 120 | global $mysoc; |
||
| 121 | |||
| 122 | // Load translation files required by the page |
||
| 123 | $langs->loadLangs(array("companies", "other", "compta", "banks", "bills", "donations", "loan", "accountancy", "trips", "salaries", "hrm", "members")); |
||
| 124 | |||
| 125 | // Multi journal |
||
| 126 | $id_journal = GETPOSTINT('id_journal'); |
||
| 127 | |||
| 128 | $date_startmonth = GETPOSTINT('date_startmonth'); |
||
| 129 | $date_startday = GETPOSTINT('date_startday'); |
||
| 130 | $date_startyear = GETPOSTINT('date_startyear'); |
||
| 131 | $date_endmonth = GETPOSTINT('date_endmonth'); |
||
| 132 | $date_endday = GETPOSTINT('date_endday'); |
||
| 133 | $date_endyear = GETPOSTINT('date_endyear'); |
||
| 134 | $in_bookkeeping = GETPOST('in_bookkeeping', 'aZ09'); |
||
| 135 | if ($in_bookkeeping == '') { |
||
| 136 | $in_bookkeeping = 'notyet'; |
||
| 137 | } |
||
| 138 | |||
| 139 | $now = dol_now(); |
||
| 140 | |||
| 141 | $action = GETPOST('action', 'aZ09'); |
||
| 142 | |||
| 143 | // Security check |
||
| 144 | if (!isModEnabled('accounting')) { |
||
| 145 | accessforbidden(); |
||
| 146 | } |
||
| 147 | if ($user->socid > 0) { |
||
| 148 | accessforbidden(); |
||
| 149 | } |
||
| 150 | if (!$user->hasRight('accounting', 'mouvements', 'lire')) { |
||
| 151 | accessforbidden(); |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /* |
||
| 156 | * Actions |
||
| 157 | */ |
||
| 158 | |||
| 159 | $error = 0; |
||
| 160 | |||
| 161 | $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); |
||
| 162 | $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); |
||
| 163 | |||
| 164 | if (empty($date_startmonth)) { |
||
| 165 | // Period by default on transfer |
||
| 166 | $dates = getDefaultDatesForTransfer(); |
||
| 167 | $date_start = $dates['date_start']; |
||
| 168 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 169 | $pastmonth = $dates['pastmonth']; |
||
| 170 | } |
||
| 171 | if (empty($date_endmonth)) { |
||
| 172 | // Period by default on transfer |
||
| 173 | $dates = getDefaultDatesForTransfer(); |
||
| 174 | $date_end = $dates['date_end']; |
||
| 175 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 176 | $pastmonth = $dates['pastmonth']; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form |
||
| 180 | $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); |
||
| 181 | $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); |
||
| 182 | } |
||
| 183 | |||
| 184 | $sql = "SELECT b.rowid, b.dateo as do, b.datev as dv, b.amount, b.amount_main_currency, b.label, b.rappro, b.num_releve, b.num_chq, b.fk_type, b.fk_account,"; |
||
| 185 | $sql .= " ba.courant, ba.ref as baref, ba.account_number, ba.fk_accountancy_journal,"; |
||
| 186 | $sql .= " soc.rowid as socid, soc.nom as name, soc.email as email, bu1.type as typeop_company,"; |
||
| 187 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 188 | $sql .= " spe.accountancy_code_customer as code_compta,"; |
||
| 189 | $sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,"; |
||
| 190 | } else { |
||
| 191 | $sql .= " soc.code_compta,"; |
||
| 192 | $sql .= " soc.code_compta_fournisseur,"; |
||
| 193 | } |
||
| 194 | $sql .= " u.accountancy_code, u.rowid as userid, u.lastname as lastname, u.firstname as firstname, u.email as useremail, u.statut as userstatus,"; |
||
| 195 | $sql .= " bu2.type as typeop_user,"; |
||
| 196 | $sql .= " bu3.type as typeop_payment, bu4.type as typeop_payment_supplier"; |
||
| 197 | $sql .= " FROM " . MAIN_DB_PREFIX . "bank as b"; |
||
| 198 | $sql .= " JOIN " . MAIN_DB_PREFIX . "bank_account as ba on b.fk_account=ba.rowid"; |
||
| 199 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu1 ON bu1.fk_bank = b.rowid AND bu1.type='company'"; |
||
| 200 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu2 ON bu2.fk_bank = b.rowid AND bu2.type='user'"; |
||
| 201 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu3 ON bu3.fk_bank = b.rowid AND bu3.type='payment'"; |
||
| 202 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "bank_url as bu4 ON bu4.fk_bank = b.rowid AND bu4.type='payment_supplier'"; |
||
| 203 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as soc on bu1.url_id=soc.rowid"; |
||
| 204 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 205 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = soc.rowid AND spe.entity = " . ((int) $conf->entity); |
||
| 206 | } |
||
| 207 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u on bu2.url_id=u.rowid"; |
||
| 208 | $sql .= " WHERE ba.fk_accountancy_journal=" . ((int) $id_journal); |
||
| 209 | $sql .= ' AND b.amount <> 0 AND ba.entity IN (' . getEntity('bank_account', 0) . ')'; // We don't share object for accountancy |
||
| 210 | if ($date_start && $date_end) { |
||
| 211 | $sql .= " AND b.dateo >= '" . $db->idate($date_start) . "' AND b.dateo <= '" . $db->idate($date_end) . "'"; |
||
| 212 | } |
||
| 213 | // Define begin binding date |
||
| 214 | if (getDolGlobalInt('ACCOUNTING_DATE_START_BINDING')) { |
||
| 215 | $sql .= " AND b.dateo >= '" . $db->idate(getDolGlobalInt('ACCOUNTING_DATE_START_BINDING')) . "'"; |
||
| 216 | } |
||
| 217 | // Already in bookkeeping or not |
||
| 218 | if ($in_bookkeeping == 'already') { |
||
| 219 | $sql .= " AND (b.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='bank') )"; |
||
| 220 | } |
||
| 221 | if ($in_bookkeeping == 'notyet') { |
||
| 222 | $sql .= " AND (b.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='bank') )"; |
||
| 223 | } |
||
| 224 | $sql .= " ORDER BY b.datev"; |
||
| 225 | //print $sql; |
||
| 226 | |||
| 227 | $object = new Account($db); |
||
| 228 | $paymentstatic = new Paiement($db); |
||
| 229 | $paymentsupplierstatic = new PaiementFourn($db); |
||
| 230 | $societestatic = new Societe($db); |
||
| 231 | $userstatic = new User($db); |
||
| 232 | $bankaccountstatic = new Account($db); |
||
| 233 | $chargestatic = new ChargeSociales($db); |
||
| 234 | $paymentdonstatic = new PaymentDonation($db); |
||
| 235 | $paymentvatstatic = new Tva($db); |
||
| 236 | $paymentsalstatic = new PaymentSalary($db); |
||
| 237 | $paymentexpensereportstatic = new PaymentExpenseReport($db); |
||
| 238 | $paymentvariousstatic = new PaymentVarious($db); |
||
| 239 | $paymentloanstatic = new PaymentLoan($db); |
||
| 240 | $accountLinestatic = new AccountLine($db); |
||
| 241 | $paymentsubscriptionstatic = new Subscription($db); |
||
| 242 | |||
| 243 | $tmppayment = new Paiement($db); |
||
| 244 | $tmpinvoice = new Facture($db); |
||
| 245 | |||
| 246 | $accountingaccount = new AccountingAccount($db); |
||
| 247 | |||
| 248 | // Get code of finance journal |
||
| 249 | $accountingjournalstatic = new AccountingJournal($db); |
||
| 250 | $accountingjournalstatic->fetch($id_journal); |
||
| 251 | $journal = $accountingjournalstatic->code; |
||
| 252 | $journal_label = $accountingjournalstatic->label; |
||
| 253 | |||
| 254 | $tabcompany = array(); |
||
| 255 | $tabuser = array(); |
||
| 256 | $tabpay = array(); |
||
| 257 | $tabbq = array(); |
||
| 258 | $tabtp = array(); |
||
| 259 | $tabtype = array(); |
||
| 260 | $tabmoreinfo = array(); |
||
| 261 | |||
| 262 | ' |
||
| 263 | @phan-var-force array<array{id:mixed,name:mixed,code_compta:string,email:string}> $tabcompany |
||
| 264 | @phan-var-force array<array{id:int,name:string,lastname:string,firstname:string,email:string,accountancy_code:string,status:int> $tabuser |
||
| 265 | @phan-var-force array<int,array{date:string,type_payment:string,ref:string,fk_bank:int,ban_account_ref:string,fk_bank_account:int,lib:string,type:string}> $tabpay |
||
| 266 | @phan-var-force array<array{lib:string,date?:int|string,type_payment?:string,ref?:string,fk_bank?:int,ban_account_ref?:string,fk_bank_account?:int,type?:string,bank_account_ref?:string,paymentid?:int,paymentsupplierid?:int,soclib?:string,paymentscid?:int,paymentdonationid?:int,paymentsubscriptionid?:int,paymentvatid?:int,paymentsalid?:int,paymentexpensereport?:int,paymentvariousid?:int,account_various?:string,paymentloanid?:int}> $tabtp |
||
| 267 | '; |
||
| 268 | |||
| 269 | //print $sql; |
||
| 270 | dol_syslog("accountancy/journal/bankjournal.php", LOG_DEBUG); |
||
| 271 | $result = $db->query($sql); |
||
| 272 | if ($result) { |
||
| 273 | $num = $db->num_rows($result); |
||
| 274 | //print $sql; |
||
| 275 | |||
| 276 | // Variables |
||
| 277 | $account_supplier = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER', 'NotDefined'); // NotDefined is a reserved word |
||
| 278 | $account_customer = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER', 'NotDefined'); // NotDefined is a reserved word |
||
| 279 | $account_employee = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT', 'NotDefined'); // NotDefined is a reserved word |
||
| 280 | $account_pay_vat = getDolGlobalString('ACCOUNTING_VAT_PAY_ACCOUNT', 'NotDefined'); // NotDefined is a reserved word |
||
| 281 | $account_pay_donation = getDolGlobalString('DONATION_ACCOUNTINGACCOUNT', 'NotDefined'); // NotDefined is a reserved word |
||
| 282 | $account_pay_subscription = getDolGlobalString('ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT', 'NotDefined'); // NotDefined is a reserved word |
||
| 283 | $account_transfer = getDolGlobalString('ACCOUNTING_ACCOUNT_TRANSFER_CASH', 'NotDefined'); // NotDefined is a reserved word |
||
| 284 | |||
| 285 | // Loop on each line into llx_bank table. For each line, we should get: |
||
| 286 | // one line tabpay = line into bank |
||
| 287 | // one line for bank record = tabbq |
||
| 288 | // one line for thirdparty record = tabtp |
||
| 289 | $i = 0; |
||
| 290 | while ($i < $num) { |
||
| 291 | $obj = $db->fetch_object($result); |
||
| 292 | |||
| 293 | $lineisapurchase = -1; |
||
| 294 | $lineisasale = -1; |
||
| 295 | // Old method to detect if it's a sale or purchase |
||
| 296 | if ($obj->label == '(SupplierInvoicePayment)' || $obj->label == '(SupplierInvoicePaymentBack)') { |
||
| 297 | $lineisapurchase = 1; |
||
| 298 | } |
||
| 299 | if ($obj->label == '(CustomerInvoicePayment)' || $obj->label == '(CustomerInvoicePaymentBack)') { |
||
| 300 | $lineisasale = 1; |
||
| 301 | } |
||
| 302 | // Try a more reliable method to detect if record is a supplier payment or a customer payment |
||
| 303 | if ($lineisapurchase < 0) { |
||
| 304 | if ($obj->typeop_payment_supplier == 'payment_supplier') { |
||
| 305 | $lineisapurchase = 1; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | if ($lineisasale < 0) { |
||
| 309 | if ($obj->typeop_payment == 'payment') { |
||
| 310 | $lineisasale = 1; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | //var_dump($obj->type_payment); //var_dump($obj->type_payment_supplier); |
||
| 314 | //var_dump($lineisapurchase); //var_dump($lineisasale); |
||
| 315 | |||
| 316 | // Set accountancy code for bank |
||
| 317 | $compta_bank = $obj->account_number; |
||
| 318 | |||
| 319 | // Set accountancy code for thirdparty (example: '411CU...' or '411' if no subledger account defined on customer) |
||
| 320 | $compta_soc = 'NotDefined'; |
||
| 321 | if ($lineisapurchase > 0) { |
||
| 322 | $compta_soc = (($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $account_supplier); |
||
| 323 | } |
||
| 324 | if ($lineisasale > 0) { |
||
| 325 | $compta_soc = (!empty($obj->code_compta) ? $obj->code_compta : $account_customer); |
||
| 326 | } |
||
| 327 | |||
| 328 | $tabcompany[$obj->rowid] = array( |
||
| 329 | 'id' => $obj->socid, |
||
| 330 | 'name' => $obj->name, |
||
| 331 | 'code_compta' => $compta_soc, |
||
| 332 | 'email' => $obj->email |
||
| 333 | ); |
||
| 334 | |||
| 335 | // Set accountancy code for user |
||
| 336 | // $obj->accountancy_code is the accountancy_code of table u=user but it is defined only if a link with type 'user' exists) |
||
| 337 | $compta_user = (!empty($obj->accountancy_code) ? $obj->accountancy_code : ''); |
||
| 338 | |||
| 339 | $tabuser[$obj->rowid] = array( |
||
| 340 | 'id' => $obj->userid, |
||
| 341 | 'name' => dolGetFirstLastname($obj->firstname, $obj->lastname), |
||
| 342 | 'lastname' => $obj->lastname, |
||
| 343 | 'firstname' => $obj->firstname, |
||
| 344 | 'email' => $obj->useremail, |
||
| 345 | 'accountancy_code' => $compta_user, |
||
| 346 | 'status' => $obj->userstatus |
||
| 347 | ); |
||
| 348 | |||
| 349 | // Variable bookkeeping ($obj->rowid is Bank Id) |
||
| 350 | $tabpay[$obj->rowid]["date"] = $db->jdate($obj->do); |
||
| 351 | $tabpay[$obj->rowid]["type_payment"] = $obj->fk_type; // CHQ, VIR, LIQ, CB, ... |
||
| 352 | $tabpay[$obj->rowid]["ref"] = $obj->label; // By default. Not unique. May be changed later |
||
| 353 | $tabpay[$obj->rowid]["fk_bank"] = $obj->rowid; |
||
| 354 | $tabpay[$obj->rowid]["bank_account_ref"] = $obj->baref; |
||
| 355 | $tabpay[$obj->rowid]["fk_bank_account"] = $obj->fk_account; |
||
| 356 | $reg = array(); |
||
| 357 | if (preg_match('/^\((.*)\)$/i', $obj->label, $reg)) { |
||
| 358 | $tabpay[$obj->rowid]["lib"] = $langs->trans($reg[1]); |
||
| 359 | } else { |
||
| 360 | $tabpay[$obj->rowid]["lib"] = dol_trunc($obj->label, 60); |
||
| 361 | } |
||
| 362 | |||
| 363 | // Load of url links to the line into llx_bank (so load llx_bank_url) |
||
| 364 | $links = $object->get_url($obj->rowid); // Get an array('url'=>, 'url_id'=>, 'label'=>, 'type'=> 'fk_bank'=> ) |
||
| 365 | |||
| 366 | // By default |
||
| 367 | $tabpay[$obj->rowid]['type'] = 'unknown'; // Can be SOLD, miscellaneous entry, payment of patient, or any old record with no links in bank_url. |
||
| 368 | $tabtype[$obj->rowid] = 'unknown'; |
||
| 369 | $tabmoreinfo[$obj->rowid] = array(); |
||
| 370 | |||
| 371 | $amounttouse = $obj->amount; |
||
| 372 | if (!empty($obj->amount_main_currency)) { |
||
| 373 | // If $obj->amount_main_currency is set, it means that $obj->amount is not in same currency, we must use $obj->amount_main_currency |
||
| 374 | $amounttouse = $obj->amount_main_currency; |
||
| 375 | } |
||
| 376 | |||
| 377 | // get_url may return -1 which is not traversable |
||
| 378 | if (is_array($links) && count($links) > 0) { |
||
| 379 | // Test if entry is for a social contribution, salary or expense report. |
||
| 380 | // In such a case, we will ignore the bank url line for user |
||
| 381 | $is_sc = false; |
||
| 382 | $is_salary = false; |
||
| 383 | $is_expensereport = false; |
||
| 384 | foreach ($links as $v) { |
||
| 385 | if ($v['type'] == 'sc') { |
||
| 386 | $is_sc = true; |
||
| 387 | break; |
||
| 388 | } |
||
| 389 | if ($v['type'] == 'payment_salary') { |
||
| 390 | $is_salary = true; |
||
| 391 | break; |
||
| 392 | } |
||
| 393 | if ($v['type'] == 'payment_expensereport') { |
||
| 394 | $is_expensereport = true; |
||
| 395 | break; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | // Now loop on each link of record in bank (code similar to bankentries_list.php) |
||
| 399 | foreach ($links as $key => $val) { |
||
| 400 | if ($links[$key]['type'] == 'user' && !$is_sc && !$is_salary && !$is_expensereport) { |
||
| 401 | continue; |
||
| 402 | } |
||
| 403 | if (in_array($links[$key]['type'], array('sc', 'payment_sc', 'payment', 'payment_supplier', 'payment_vat', 'payment_expensereport', 'banktransfert', 'payment_donation', 'member', 'payment_loan', 'payment_salary', 'payment_various'))) { |
||
| 404 | // So we excluded 'company' and 'user' here. We want only payment lines |
||
| 405 | |||
| 406 | // We save tabtype for a future use, to remember what kind of payment it is |
||
| 407 | $tabpay[$obj->rowid]['type'] = $links[$key]['type']; |
||
| 408 | $tabtype[$obj->rowid] = $links[$key]['type']; |
||
| 409 | /* phpcs:disable -- Code does nothing at this moment -> commented |
||
| 410 | } elseif (in_array($links[$key]['type'], array('company', 'user'))) { |
||
| 411 | if ($tabpay[$obj->rowid]['type'] == 'unknown') { |
||
| 412 | // We can guess here it is a bank record for a thirdparty company or a user. |
||
| 413 | // But we won't be able to record somewhere else than into a waiting account, because there is no other journal to record the contreparty. |
||
| 414 | } |
||
| 415 | */ // phpcs::enable |
||
| 416 | } |
||
| 417 | |||
| 418 | // Special case to ask later to add more request to get information for old links without company link. |
||
| 419 | if ($links[$key]['type'] == 'withdraw') { |
||
| 420 | $tabmoreinfo[$obj->rowid]['withdraw'] = 1; |
||
| 421 | } |
||
| 422 | |||
| 423 | if ($links[$key]['type'] == 'payment') { |
||
| 424 | $paymentstatic->id = $links[$key]['url_id']; |
||
| 425 | $paymentstatic->ref = $links[$key]['url_id']; |
||
| 426 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this |
||
| 427 | $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id; |
||
| 428 | } elseif ($links[$key]['type'] == 'payment_supplier') { |
||
| 429 | $paymentsupplierstatic->id = $links[$key]['url_id']; |
||
| 430 | $paymentsupplierstatic->ref = $links[$key]['url_id']; |
||
| 431 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsupplierstatic->getNomUrl(2); |
||
| 432 | $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id; |
||
| 433 | } elseif ($links[$key]['type'] == 'company') { |
||
| 434 | $societestatic->id = $links[$key]['url_id']; |
||
| 435 | $societestatic->name = $links[$key]['label']; |
||
| 436 | $societestatic->email = $tabcompany[$obj->rowid]['email']; |
||
| 437 | $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30); |
||
| 438 | if ($compta_soc) { |
||
| 439 | if (empty($tabtp[$obj->rowid][$compta_soc])) { |
||
| 440 | $tabtp[$obj->rowid][$compta_soc] = $amounttouse; |
||
| 441 | } else { |
||
| 442 | $tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | } elseif ($links[$key]['type'] == 'user') { |
||
| 446 | $userstatic->id = $links[$key]['url_id']; |
||
| 447 | $userstatic->name = $links[$key]['label']; |
||
| 448 | $userstatic->email = $tabuser[$obj->rowid]['email']; |
||
| 449 | $userstatic->firstname = $tabuser[$obj->rowid]['firstname']; |
||
| 450 | $userstatic->lastname = $tabuser[$obj->rowid]['lastname']; |
||
| 451 | $userstatic->statut = $tabuser[$obj->rowid]['status']; |
||
| 452 | $userstatic->status = $tabuser[$obj->rowid]['status']; |
||
| 453 | $userstatic->accountancy_code = $tabuser[$obj->rowid]['accountancy_code']; |
||
| 454 | if ($userstatic->id > 0) { |
||
| 455 | $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
||
| 456 | } else { |
||
| 457 | $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen, but happens with old data when id of user was not saved on expense report payment. |
||
| 458 | } |
||
| 459 | if ($compta_user) { |
||
| 460 | $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
||
| 461 | } |
||
| 462 | } elseif ($links[$key]['type'] == 'sc') { |
||
| 463 | $chargestatic->id = $links[$key]['url_id']; |
||
| 464 | $chargestatic->ref = $links[$key]['url_id']; |
||
| 465 | |||
| 466 | $tabpay[$obj->rowid]["lib"] .= ' '.$chargestatic->getNomUrl(2); |
||
| 467 | $reg = array(); |
||
| 468 | if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) { |
||
| 469 | if ($reg[1] == 'socialcontribution') { |
||
| 470 | $reg[1] = 'SocialContribution'; |
||
| 471 | } |
||
| 472 | $chargestatic->label = $langs->trans($reg[1]); |
||
| 473 | } else { |
||
| 474 | $chargestatic->label = $links[$key]['label']; |
||
| 475 | } |
||
| 476 | $chargestatic->ref = $chargestatic->label; |
||
| 477 | $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30); |
||
| 478 | $tabpay[$obj->rowid]["paymentscid"] = $chargestatic->id; |
||
| 479 | |||
| 480 | // Retrieve the accounting code of the social contribution of the payment from link of payment. |
||
| 481 | // Note: We have the social contribution id, it can be faster to get accounting code from social contribution id. |
||
| 482 | $sqlmid = "SELECT cchgsoc.accountancy_code"; |
||
| 483 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."c_chargesociales cchgsoc"; |
||
| 484 | $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."chargesociales as chgsoc ON chgsoc.fk_type = cchgsoc.id"; |
||
| 485 | $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."paiementcharge as paycharg ON paycharg.fk_charge = chgsoc.rowid"; |
||
| 486 | $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."bank_url as bkurl ON bkurl.url_id=paycharg.rowid AND bkurl.type = 'payment_sc'"; |
||
| 487 | $sqlmid .= " WHERE bkurl.fk_bank = ".((int) $obj->rowid); |
||
| 488 | |||
| 489 | dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
||
| 490 | $resultmid = $db->query($sqlmid); |
||
| 491 | if ($resultmid) { |
||
| 492 | $objmid = $db->fetch_object($resultmid); |
||
| 493 | $tabtp[$obj->rowid][$objmid->accountancy_code] += $amounttouse; |
||
| 494 | } |
||
| 495 | } elseif ($links[$key]['type'] == 'payment_donation') { |
||
| 496 | $paymentdonstatic->id = $links[$key]['url_id']; |
||
| 497 | $paymentdonstatic->ref = $links[$key]['url_id']; |
||
| 498 | $paymentdonstatic->fk_donation = $links[$key]['url_id']; |
||
| 499 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentdonstatic->getNomUrl(2); |
||
| 500 | $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id; |
||
| 501 | $tabtp[$obj->rowid][$account_pay_donation] += $amounttouse; |
||
| 502 | } elseif ($links[$key]['type'] == 'member') { |
||
| 503 | $paymentsubscriptionstatic->id = $links[$key]['url_id']; |
||
| 504 | $paymentsubscriptionstatic->ref = $links[$key]['url_id']; |
||
| 505 | $paymentsubscriptionstatic->label = $links[$key]['label']; |
||
| 506 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsubscriptionstatic->getNomUrl(2); |
||
| 507 | $tabpay[$obj->rowid]["paymentsubscriptionid"] = $paymentsubscriptionstatic->id; |
||
| 508 | $paymentsubscriptionstatic->fetch($paymentsubscriptionstatic->id); |
||
| 509 | $tabtp[$obj->rowid][$account_pay_subscription] += $amounttouse; |
||
| 510 | } elseif ($links[$key]['type'] == 'payment_vat') { // Payment VAT |
||
| 511 | $paymentvatstatic->id = $links[$key]['url_id']; |
||
| 512 | $paymentvatstatic->ref = $links[$key]['url_id']; |
||
| 513 | $paymentvatstatic->label = $links[$key]['label']; |
||
| 514 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvatstatic->getNomUrl(2); |
||
| 515 | $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id; |
||
| 516 | $tabtp[$obj->rowid][$account_pay_vat] += $amounttouse; |
||
| 517 | } elseif ($links[$key]['type'] == 'payment_salary') { |
||
| 518 | $paymentsalstatic->id = $links[$key]['url_id']; |
||
| 519 | $paymentsalstatic->ref = $links[$key]['url_id']; |
||
| 520 | $paymentsalstatic->label = $links[$key]['label']; |
||
| 521 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsalstatic->getNomUrl(2); |
||
| 522 | $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id; |
||
| 523 | |||
| 524 | // This part of code is no more required. it is here to solve case where a link were missing (with v14.0.0) and keep writing in accountancy complete. |
||
| 525 | // Note: A better way to fix this is to delete payment of salary and recreate it, or to fix the bookkeeping table manually after. |
||
| 526 | if (getDolGlobalString('ACCOUNTANCY_AUTOFIX_MISSING_LINK_TO_USER_ON_SALARY_BANK_PAYMENT')) { |
||
| 527 | $tmpsalary = new Salary($db); |
||
| 528 | $tmpsalary->fetch($paymentsalstatic->id); |
||
| 529 | $tmpsalary->fetch_user($tmpsalary->fk_user); |
||
| 530 | |||
| 531 | $userstatic->id = $tmpsalary->user->id; |
||
| 532 | $userstatic->name = $tmpsalary->user->name; |
||
| 533 | $userstatic->email = $tmpsalary->user->email; |
||
| 534 | $userstatic->firstname = $tmpsalary->user->firstname; |
||
| 535 | $userstatic->lastname = $tmpsalary->user->lastname; |
||
| 536 | $userstatic->statut = $tmpsalary->user->status; |
||
| 537 | $userstatic->accountancy_code = $tmpsalary->user->accountancy_code; |
||
| 538 | |||
| 539 | if ($userstatic->id > 0) { |
||
| 540 | $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
||
| 541 | } else { |
||
| 542 | $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen |
||
| 543 | } |
||
| 544 | |||
| 545 | if (empty($obj->typeop_user)) { // Add test to avoid to add amount twice if a link already exists also on user. |
||
| 546 | $compta_user = $userstatic->accountancy_code; |
||
| 547 | if ($compta_user) { |
||
| 548 | $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
||
| 549 | $tabuser[$obj->rowid] = array( |
||
| 550 | 'id' => $userstatic->id, |
||
| 551 | 'name' => dolGetFirstLastname($userstatic->firstname, $userstatic->lastname), |
||
| 552 | 'lastname' => $userstatic->lastname, |
||
| 553 | 'firstname' => $userstatic->firstname, |
||
| 554 | 'email' => $userstatic->email, |
||
| 555 | 'accountancy_code' => $compta_user, |
||
| 556 | 'status' => $userstatic->status |
||
| 557 | ); |
||
| 558 | } |
||
| 559 | } |
||
| 560 | } |
||
| 561 | } elseif ($links[$key]['type'] == 'payment_expensereport') { |
||
| 562 | $paymentexpensereportstatic->id = $links[$key]['url_id']; |
||
| 563 | $tabpay[$obj->rowid]["lib"] .= $paymentexpensereportstatic->getNomUrl(2); |
||
| 564 | $tabpay[$obj->rowid]["paymentexpensereport"] = $paymentexpensereportstatic->id; |
||
| 565 | } elseif ($links[$key]['type'] == 'payment_various') { |
||
| 566 | $paymentvariousstatic->id = $links[$key]['url_id']; |
||
| 567 | $paymentvariousstatic->ref = $links[$key]['url_id']; |
||
| 568 | $paymentvariousstatic->label = $links[$key]['label']; |
||
| 569 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvariousstatic->getNomUrl(2); |
||
| 570 | $tabpay[$obj->rowid]["paymentvariousid"] = $paymentvariousstatic->id; |
||
| 571 | $paymentvariousstatic->fetch($paymentvariousstatic->id); |
||
| 572 | $account_various = (!empty($paymentvariousstatic->accountancy_code) ? $paymentvariousstatic->accountancy_code : 'NotDefined'); // NotDefined is a reserved word |
||
| 573 | $account_subledger = (!empty($paymentvariousstatic->subledger_account) ? $paymentvariousstatic->subledger_account : ''); // NotDefined is a reserved word |
||
| 574 | $tabpay[$obj->rowid]["account_various"] = $account_various; |
||
| 575 | $tabtp[$obj->rowid][$account_subledger] += $amounttouse; |
||
| 576 | } elseif ($links[$key]['type'] == 'payment_loan') { |
||
| 577 | $paymentloanstatic->id = $links[$key]['url_id']; |
||
| 578 | $paymentloanstatic->ref = $links[$key]['url_id']; |
||
| 579 | $paymentloanstatic->fk_loan = $links[$key]['url_id']; |
||
| 580 | $tabpay[$obj->rowid]["lib"] .= ' '.$paymentloanstatic->getNomUrl(2); |
||
| 581 | $tabpay[$obj->rowid]["paymentloanid"] = $paymentloanstatic->id; |
||
| 582 | //$tabtp[$obj->rowid][$account_pay_loan] += $amounttouse; |
||
| 583 | $sqlmid = 'SELECT pl.amount_capital, pl.amount_insurance, pl.amount_interest, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest'; |
||
| 584 | $sqlmid .= ' FROM '.MAIN_DB_PREFIX.'payment_loan as pl, '.MAIN_DB_PREFIX.'loan as l'; |
||
| 585 | $sqlmid .= ' WHERE l.rowid = pl.fk_loan AND pl.fk_bank = '.((int) $obj->rowid); |
||
| 586 | |||
| 587 | dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
||
| 588 | $resultmid = $db->query($sqlmid); |
||
| 589 | if ($resultmid) { |
||
| 590 | $objmid = $db->fetch_object($resultmid); |
||
| 591 | $tabtp[$obj->rowid][$objmid->accountancy_account_capital] -= $objmid->amount_capital; |
||
| 592 | $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] -= $objmid->amount_insurance; |
||
| 593 | $tabtp[$obj->rowid][$objmid->accountancy_account_interest] -= $objmid->amount_interest; |
||
| 594 | } |
||
| 595 | } elseif ($links[$key]['type'] == 'banktransfert') { |
||
| 596 | $accountLinestatic->fetch($links[$key]['url_id']); |
||
| 597 | $tabpay[$obj->rowid]["lib"] .= ' '.$langs->trans("BankTransfer").'- '.$accountLinestatic ->getNomUrl(1); |
||
| 598 | $tabtp[$obj->rowid][$account_transfer] += $amounttouse; |
||
| 599 | $bankaccountstatic->fetch($tabpay[$obj->rowid]['fk_bank_account']); |
||
| 600 | $tabpay[$obj->rowid]["soclib"] = $bankaccountstatic->getNomUrl(2); |
||
| 601 | } |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | if (empty($tabbq[$obj->rowid][$compta_bank])) { |
||
| 606 | $tabbq[$obj->rowid][$compta_bank] = $amounttouse; |
||
| 607 | } else { |
||
| 608 | $tabbq[$obj->rowid][$compta_bank] += $amounttouse; |
||
| 609 | } |
||
| 610 | |||
| 611 | // If no links were found to know the amount on thirdparty, we try to guess it. |
||
| 612 | // This may happens on bank entries without the links lines to 'company'. |
||
| 613 | if (empty($tabtp[$obj->rowid]) && !empty($tabmoreinfo[$obj->rowid]['withdraw'])) { // If we don't find 'company' link because it is an old 'withdraw' record |
||
| 614 | foreach ($links as $key => $val) { |
||
| 615 | if ($links[$key]['type'] == 'payment') { |
||
| 616 | // Get thirdparty |
||
| 617 | $tmppayment->fetch($links[$key]['url_id']); |
||
| 618 | $arrayofamounts = $tmppayment->getAmountsArray(); |
||
| 619 | if (is_array($arrayofamounts)) { |
||
| 620 | foreach ($arrayofamounts as $invoiceid => $amount) { |
||
| 621 | $tmpinvoice->fetch($invoiceid); |
||
| 622 | $tmpinvoice->fetch_thirdparty(); |
||
| 623 | if ($tmpinvoice->thirdparty->code_compta_client) { |
||
| 624 | $tabtp[$obj->rowid][$tmpinvoice->thirdparty->code_compta_client] += $amount; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | } |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | |||
| 632 | // If no links were found to know the amount on thirdparty/user, we init it to account 'NotDefined'. |
||
| 633 | if (empty($tabtp[$obj->rowid])) { |
||
| 634 | $tabtp[$obj->rowid]['NotDefined'] = $tabbq[$obj->rowid][$compta_bank]; |
||
| 635 | } |
||
| 636 | |||
| 637 | // Check account number is ok |
||
| 638 | /*if ($action == 'writebookkeeping') // Make test now in such a case |
||
| 639 | { |
||
| 640 | reset($tabbq[$obj->rowid]); |
||
| 641 | $first_key_tabbq = key($tabbq[$obj->rowid]); |
||
| 642 | if (empty($first_key_tabbq)) |
||
| 643 | { |
||
| 644 | $error++; |
||
| 645 | setEventMessages($langs->trans('ErrorAccountancyCodeOnBankAccountNotDefined', $obj->baref), null, 'errors'); |
||
| 646 | } |
||
| 647 | reset($tabtp[$obj->rowid]); |
||
| 648 | $first_key_tabtp = key($tabtp[$obj->rowid]); |
||
| 649 | if (empty($first_key_tabtp)) |
||
| 650 | { |
||
| 651 | $error++; |
||
| 652 | setEventMessages($langs->trans('ErrorAccountancyCodeOnThirdPartyNotDefined'), null, 'errors'); |
||
| 653 | } |
||
| 654 | }*/ |
||
| 655 | |||
| 656 | // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
||
| 657 | |||
| 658 | $i++; |
||
| 659 | } |
||
| 660 | } else { |
||
| 661 | dol_print_error($db); |
||
| 662 | } |
||
| 663 | |||
| 664 | |||
| 665 | //var_dump($tabpay); |
||
| 666 | //var_dump($tabcompany); |
||
| 667 | //var_dump($tabbq); |
||
| 668 | //var_dump($tabtp); |
||
| 669 | //var_dump($tabtype); |
||
| 670 | |||
| 671 | // Write bookkeeping |
||
| 672 | if (!$error && $action == 'writebookkeeping') { |
||
| 673 | $now = dol_now(); |
||
| 674 | |||
| 675 | $accountingaccountcustomer = new AccountingAccount($db); |
||
| 676 | $accountingaccountcustomer->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), true); |
||
| 677 | |||
| 678 | $accountingaccountsupplier = new AccountingAccount($db); |
||
| 679 | $accountingaccountsupplier->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), true); |
||
| 680 | |||
| 681 | $accountingaccountpayment = new AccountingAccount($db); |
||
| 682 | $accountingaccountpayment->fetch(null, getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'), true); |
||
| 683 | |||
| 684 | $accountingaccountsuspense = new AccountingAccount($db); |
||
| 685 | $accountingaccountsuspense->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'), true); |
||
| 686 | |||
| 687 | $error = 0; |
||
| 688 | foreach ($tabpay as $key => $val) { // $key is rowid into llx_bank |
||
| 689 | $date = dol_print_date($val["date"], 'day'); |
||
| 690 | |||
| 691 | $ref = getSourceDocRef($val, $tabtype[$key]); |
||
| 692 | |||
| 693 | $errorforline = 0; |
||
| 694 | |||
| 695 | $totalcredit = 0; |
||
| 696 | $totaldebit = 0; |
||
| 697 | |||
| 698 | $db->begin(); |
||
| 699 | |||
| 700 | // Introduce a protection. Total of tabtp must be total of tabbq |
||
| 701 | //var_dump($tabpay); |
||
| 702 | //var_dump($tabtp); |
||
| 703 | //var_dump($tabbq);exit; |
||
| 704 | |||
| 705 | // Bank |
||
| 706 | if (!$errorforline && is_array($tabbq[$key])) { |
||
| 707 | // Line into bank account |
||
| 708 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 709 | if ($mt) { |
||
| 710 | $accountingaccount->fetch(null, $k, true); // $k is accounting bank account. TODO We should use a cache here to avoid this fetch |
||
| 711 | $account_label = $accountingaccount->label; |
||
| 712 | |||
| 713 | $reflabel = ''; |
||
| 714 | if (!empty($val['lib'])) { |
||
| 715 | $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
||
| 716 | } |
||
| 717 | $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
||
| 718 | if (!empty($val['soclib'])) { |
||
| 719 | $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
||
| 720 | } |
||
| 721 | |||
| 722 | $bookkeeping = new BookKeeping($db); |
||
| 723 | $bookkeeping->doc_date = $val["date"]; |
||
| 724 | $bookkeeping->doc_ref = $ref; |
||
| 725 | $bookkeeping->doc_type = 'bank'; |
||
| 726 | $bookkeeping->fk_doc = $key; |
||
| 727 | $bookkeeping->fk_docdet = $val["fk_bank"]; |
||
| 728 | |||
| 729 | $bookkeeping->numero_compte = $k; |
||
| 730 | $bookkeeping->label_compte = $account_label; |
||
| 731 | |||
| 732 | $bookkeeping->label_operation = $reflabel; |
||
| 733 | $bookkeeping->montant = $mt; |
||
| 734 | $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; |
||
| 735 | $bookkeeping->debit = ($mt >= 0 ? $mt : 0); |
||
| 736 | $bookkeeping->credit = ($mt < 0 ? -$mt : 0); |
||
| 737 | $bookkeeping->code_journal = $journal; |
||
| 738 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 739 | $bookkeeping->fk_user_author = $user->id; |
||
| 740 | $bookkeeping->date_creation = $now; |
||
| 741 | |||
| 742 | // No subledger_account value for the bank line but add a specific label_operation |
||
| 743 | $bookkeeping->subledger_account = ''; |
||
| 744 | $bookkeeping->label_operation = $reflabel; |
||
| 745 | $bookkeeping->entity = $conf->entity; |
||
| 746 | |||
| 747 | $totaldebit += $bookkeeping->debit; |
||
| 748 | $totalcredit += $bookkeeping->credit; |
||
| 749 | |||
| 750 | $result = $bookkeeping->create($user); |
||
| 751 | if ($result < 0) { |
||
| 752 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 753 | $error++; |
||
| 754 | $errorforline++; |
||
| 755 | setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 756 | } else { |
||
| 757 | $error++; |
||
| 758 | $errorforline++; |
||
| 759 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 760 | } |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | } |
||
| 765 | |||
| 766 | // Third party |
||
| 767 | if (!$errorforline) { |
||
| 768 | if (is_array($tabtp[$key])) { |
||
| 769 | // Line into thirdparty account |
||
| 770 | foreach ($tabtp[$key] as $k => $mt) { |
||
| 771 | if ($mt) { |
||
| 772 | $lettering = false; |
||
| 773 | |||
| 774 | $reflabel = ''; |
||
| 775 | if (!empty($val['lib'])) { |
||
| 776 | $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
||
| 777 | } |
||
| 778 | if ($tabtype[$key] == 'banktransfert') { |
||
| 779 | $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
||
| 780 | } else { |
||
| 781 | $reflabel .= dol_string_nohtmltag($val['soclib']); |
||
| 782 | } |
||
| 783 | |||
| 784 | $bookkeeping = new BookKeeping($db); |
||
| 785 | $bookkeeping->doc_date = $val["date"]; |
||
| 786 | $bookkeeping->doc_ref = $ref; |
||
| 787 | $bookkeeping->doc_type = 'bank'; |
||
| 788 | $bookkeeping->fk_doc = $key; |
||
| 789 | $bookkeeping->fk_docdet = $val["fk_bank"]; |
||
| 790 | |||
| 791 | $bookkeeping->label_operation = $reflabel; |
||
| 792 | $bookkeeping->montant = $mt; |
||
| 793 | $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
||
| 794 | $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
||
| 795 | $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
||
| 796 | $bookkeeping->code_journal = $journal; |
||
| 797 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 798 | $bookkeeping->fk_user_author = $user->id; |
||
| 799 | $bookkeeping->date_creation = $now; |
||
| 800 | |||
| 801 | if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice |
||
| 802 | $lettering = true; |
||
| 803 | $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
||
| 804 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
||
| 805 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
||
| 806 | $bookkeeping->label_compte = $accountingaccountcustomer->label; |
||
| 807 | } elseif ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice |
||
| 808 | $lettering = true; |
||
| 809 | $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
||
| 810 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
||
| 811 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
||
| 812 | $bookkeeping->label_compte = $accountingaccountsupplier->label; |
||
| 813 | } elseif ($tabtype[$key] == 'payment_expensereport') { |
||
| 814 | $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
||
| 815 | $bookkeeping->subledger_label = $tabuser[$key]['name']; |
||
| 816 | $bookkeeping->numero_compte = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
||
| 817 | $bookkeeping->label_compte = $accountingaccountpayment->label; |
||
| 818 | } elseif ($tabtype[$key] == 'payment_salary') { |
||
| 819 | $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
||
| 820 | $bookkeeping->subledger_label = $tabuser[$key]['name']; |
||
| 821 | $bookkeeping->numero_compte = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
||
| 822 | $bookkeeping->label_compte = $accountingaccountpayment->label; |
||
| 823 | } elseif (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution |
||
| 824 | $bookkeeping->subledger_account = ''; |
||
| 825 | $bookkeeping->subledger_label = ''; |
||
| 826 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 827 | $bookkeeping->numero_compte = $k; |
||
| 828 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 829 | } elseif ($tabtype[$key] == 'payment_vat') { |
||
| 830 | $bookkeeping->subledger_account = ''; |
||
| 831 | $bookkeeping->subledger_label = ''; |
||
| 832 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 833 | $bookkeeping->numero_compte = $k; |
||
| 834 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 835 | } elseif ($tabtype[$key] == 'payment_donation') { |
||
| 836 | $bookkeeping->subledger_account = ''; |
||
| 837 | $bookkeeping->subledger_label = ''; |
||
| 838 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 839 | $bookkeeping->numero_compte = $k; |
||
| 840 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 841 | } elseif ($tabtype[$key] == 'member') { |
||
| 842 | $bookkeeping->subledger_account = ''; |
||
| 843 | $bookkeeping->subledger_label = ''; |
||
| 844 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 845 | $bookkeeping->numero_compte = $k; |
||
| 846 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 847 | } elseif ($tabtype[$key] == 'payment_loan') { |
||
| 848 | $bookkeeping->subledger_account = ''; |
||
| 849 | $bookkeeping->subledger_label = ''; |
||
| 850 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 851 | $bookkeeping->numero_compte = $k; |
||
| 852 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 853 | } elseif ($tabtype[$key] == 'payment_various') { |
||
| 854 | $bookkeeping->subledger_account = $k; |
||
| 855 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 856 | $accountingaccount->fetch(null, $tabpay[$key]["account_various"], true); // TODO Use a cache |
||
| 857 | $bookkeeping->numero_compte = $tabpay[$key]["account_various"]; |
||
| 858 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 859 | } elseif ($tabtype[$key] == 'banktransfert') { |
||
| 860 | $bookkeeping->subledger_account = ''; |
||
| 861 | $bookkeeping->subledger_label = ''; |
||
| 862 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 863 | $bookkeeping->numero_compte = $k; |
||
| 864 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 865 | } else { |
||
| 866 | if ($tabtype[$key] == 'unknown') { // Unknown transaction, we will use a waiting account for thirdparty. |
||
| 867 | // Temporary account |
||
| 868 | $bookkeeping->subledger_account = ''; |
||
| 869 | $bookkeeping->subledger_label = ''; |
||
| 870 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'); |
||
| 871 | $bookkeeping->label_compte = $accountingaccountsuspense->label; |
||
| 872 | } |
||
| 873 | } |
||
| 874 | $bookkeeping->label_operation = $reflabel; |
||
| 875 | $bookkeeping->entity = $conf->entity; |
||
| 876 | |||
| 877 | $totaldebit += $bookkeeping->debit; |
||
| 878 | $totalcredit += $bookkeeping->credit; |
||
| 879 | |||
| 880 | $result = $bookkeeping->create($user); |
||
| 881 | if ($result < 0) { |
||
| 882 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 883 | $error++; |
||
| 884 | $errorforline++; |
||
| 885 | setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 886 | } else { |
||
| 887 | $error++; |
||
| 888 | $errorforline++; |
||
| 889 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 890 | } |
||
| 891 | } else { |
||
| 892 | if ($lettering && getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && getDolGlobalInt('ACCOUNTING_ENABLE_AUTOLETTERING')) { |
||
| 893 | require_once DOL_DOCUMENT_ROOT . '/accountancy/class/lettering.class.php'; |
||
| 894 | $lettering_static = new Lettering($db); |
||
| 895 | $nb_lettering = $lettering_static->bookkeepingLetteringAll(array($bookkeeping->id)); |
||
| 896 | } |
||
| 897 | } |
||
| 898 | } |
||
| 899 | } |
||
| 900 | } else { // If thirdparty unknown, output the waiting account |
||
| 901 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 902 | if ($mt) { |
||
| 903 | $reflabel = ''; |
||
| 904 | if (!empty($val['lib'])) { |
||
| 905 | $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
||
| 906 | } |
||
| 907 | $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
||
| 908 | |||
| 909 | $bookkeeping = new BookKeeping($db); |
||
| 910 | $bookkeeping->doc_date = $val["date"]; |
||
| 911 | $bookkeeping->doc_ref = $ref; |
||
| 912 | $bookkeeping->doc_type = 'bank'; |
||
| 913 | $bookkeeping->fk_doc = $key; |
||
| 914 | $bookkeeping->fk_docdet = $val["fk_bank"]; |
||
| 915 | $bookkeeping->montant = $mt; |
||
| 916 | $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
||
| 917 | $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
||
| 918 | $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
||
| 919 | $bookkeeping->code_journal = $journal; |
||
| 920 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 921 | $bookkeeping->fk_user_author = $user->id; |
||
| 922 | $bookkeeping->date_creation = $now; |
||
| 923 | $bookkeeping->label_compte = ''; |
||
| 924 | $bookkeeping->label_operation = $reflabel; |
||
| 925 | $bookkeeping->entity = $conf->entity; |
||
| 926 | |||
| 927 | $totaldebit += $bookkeeping->debit; |
||
| 928 | $totalcredit += $bookkeeping->credit; |
||
| 929 | |||
| 930 | $result = $bookkeeping->create($user); |
||
| 931 | |||
| 932 | if ($result < 0) { |
||
| 933 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 934 | $error++; |
||
| 935 | $errorforline++; |
||
| 936 | setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 937 | } else { |
||
| 938 | $error++; |
||
| 939 | $errorforline++; |
||
| 940 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 941 | } |
||
| 942 | } |
||
| 943 | } |
||
| 944 | } |
||
| 945 | } |
||
| 946 | } |
||
| 947 | |||
| 948 | if (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')) { |
||
| 949 | $error++; |
||
| 950 | $errorforline++; |
||
| 951 | setEventMessages('We tried to insert a non balanced transaction in book for '.$ref.'. Canceled. Surely a bug.', null, 'errors'); |
||
| 952 | } |
||
| 953 | |||
| 954 | if (!$errorforline) { |
||
| 955 | $db->commit(); |
||
| 956 | } else { |
||
| 957 | //print 'KO for line '.$key.' '.$error.'<br>'; |
||
| 958 | $db->rollback(); |
||
| 959 | |||
| 960 | $MAXNBERRORS = 5; |
||
| 961 | if ($error >= $MAXNBERRORS) { |
||
| 962 | setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped").' (>'.$MAXNBERRORS.')', null, 'errors'); |
||
| 963 | break; // Break in the foreach |
||
| 964 | } |
||
| 965 | } |
||
| 966 | } |
||
| 967 | |||
| 968 | if (empty($error) && count($tabpay) > 0) { |
||
| 969 | setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
||
| 970 | } elseif (count($tabpay) == $error) { |
||
| 971 | setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
||
| 972 | } else { |
||
| 973 | setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
||
| 974 | } |
||
| 975 | |||
| 976 | $action = ''; |
||
| 977 | |||
| 978 | // Must reload data, so we make a redirect |
||
| 979 | if (count($tabpay) != $error) { |
||
| 980 | $param = 'id_journal='.$id_journal; |
||
| 981 | $param .= '&date_startday='.$date_startday; |
||
| 982 | $param .= '&date_startmonth='.$date_startmonth; |
||
| 983 | $param .= '&date_startyear='.$date_startyear; |
||
| 984 | $param .= '&date_endday='.$date_endday; |
||
| 985 | $param .= '&date_endmonth='.$date_endmonth; |
||
| 986 | $param .= '&date_endyear='.$date_endyear; |
||
| 987 | $param .= '&in_bookkeeping='.$in_bookkeeping; |
||
| 988 | header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : '')); |
||
| 989 | exit; |
||
| 990 | } |
||
| 991 | } |
||
| 992 | |||
| 993 | |||
| 994 | |||
| 995 | // Export |
||
| 996 | if ($action == 'exportcsv') { // ISO and not UTF8 ! |
||
| 997 | $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
||
| 998 | |||
| 999 | $filename = 'journal'; |
||
| 1000 | $type_export = 'journal'; |
||
| 1001 | include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php'; |
||
| 1002 | |||
| 1003 | // CSV header line |
||
| 1004 | print '"'.$langs->transnoentitiesnoconv("BankId").'"'.$sep; |
||
| 1005 | print '"'.$langs->transnoentitiesnoconv("Date").'"'.$sep; |
||
| 1006 | print '"'.$langs->transnoentitiesnoconv("PaymentMode").'"'.$sep; |
||
| 1007 | print '"'.$langs->transnoentitiesnoconv("AccountAccounting").'"'.$sep; |
||
| 1008 | print '"'.$langs->transnoentitiesnoconv("LedgerAccount").'"'.$sep; |
||
| 1009 | print '"'.$langs->transnoentitiesnoconv("SubledgerAccount").'"'.$sep; |
||
| 1010 | print '"'.$langs->transnoentitiesnoconv("Label").'"'.$sep; |
||
| 1011 | print '"'.$langs->transnoentitiesnoconv("AccountingDebit").'"'.$sep; |
||
| 1012 | print '"'.$langs->transnoentitiesnoconv("AccountingCredit").'"'.$sep; |
||
| 1013 | print '"'.$langs->transnoentitiesnoconv("Journal").'"'.$sep; |
||
| 1014 | print '"'.$langs->transnoentitiesnoconv("Note").'"'.$sep; |
||
| 1015 | print "\n"; |
||
| 1016 | |||
| 1017 | foreach ($tabpay as $key => $val) { |
||
| 1018 | $date = dol_print_date($val["date"], 'day'); |
||
| 1019 | |||
| 1020 | $ref = getSourceDocRef($val, $tabtype[$key]); |
||
| 1021 | |||
| 1022 | // Bank |
||
| 1023 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 1024 | if ($mt) { |
||
| 1025 | $reflabel = ''; |
||
| 1026 | if (!empty($val['lib'])) { |
||
| 1027 | $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
||
| 1028 | } |
||
| 1029 | $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
||
| 1030 | if (!empty($val['soclib'])) { |
||
| 1031 | $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | print '"'.$key.'"'.$sep; |
||
| 1035 | print '"'.$date.'"'.$sep; |
||
| 1036 | print '"'.$val["type_payment"].'"'.$sep; |
||
| 1037 | print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
||
| 1038 | print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
||
| 1039 | print " ".$sep; |
||
| 1040 | print '"'.$reflabel.'"'.$sep; |
||
| 1041 | print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
||
| 1042 | print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
||
| 1043 | print '"'.$journal.'"'.$sep; |
||
| 1044 | print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
||
| 1045 | print "\n"; |
||
| 1046 | } |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | // Third party |
||
| 1050 | if (is_array($tabtp[$key])) { |
||
| 1051 | foreach ($tabtp[$key] as $k => $mt) { |
||
| 1052 | if ($mt) { |
||
| 1053 | $reflabel = ''; |
||
| 1054 | if (!empty($val['lib'])) { |
||
| 1055 | $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
||
| 1056 | } |
||
| 1057 | if ($tabtype[$key] == 'banktransfert') { |
||
| 1058 | $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
||
| 1059 | } else { |
||
| 1060 | $reflabel .= dol_string_nohtmltag($val['soclib']); |
||
| 1061 | } |
||
| 1062 | |||
| 1063 | print '"'.$key.'"'.$sep; |
||
| 1064 | print '"'.$date.'"'.$sep; |
||
| 1065 | print '"'.$val["type_payment"].'"'.$sep; |
||
| 1066 | print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
||
| 1067 | if ($tabtype[$key] == 'payment_supplier') { |
||
| 1068 | print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER').'"'.$sep; |
||
| 1069 | } elseif ($tabtype[$key] == 'payment') { |
||
| 1070 | print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER').'"'.$sep; |
||
| 1071 | } elseif ($tabtype[$key] == 'payment_expensereport') { |
||
| 1072 | print '"'.getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT').'"'.$sep; |
||
| 1073 | } elseif ($tabtype[$key] == 'payment_salary') { |
||
| 1074 | print '"'.getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT').'"'.$sep; |
||
| 1075 | } else { |
||
| 1076 | print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
||
| 1077 | } |
||
| 1078 | print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
||
| 1079 | print '"'.$reflabel.'"'.$sep; |
||
| 1080 | print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
||
| 1081 | print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
||
| 1082 | print '"'.$journal.'"'.$sep; |
||
| 1083 | print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
||
| 1084 | print "\n"; |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | } else { // If thirdparty unknown, output the waiting account |
||
| 1088 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 1089 | if ($mt) { |
||
| 1090 | $reflabel = ''; |
||
| 1091 | if (!empty($val['lib'])) { |
||
| 1092 | $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
||
| 1093 | } |
||
| 1094 | $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
||
| 1095 | |||
| 1096 | print '"'.$key.'"'.$sep; |
||
| 1097 | print '"'.$date.'"'.$sep; |
||
| 1098 | print '"'.$val["type_payment"].'"'.$sep; |
||
| 1099 | print '"'.length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
||
| 1100 | print '"'.length_accounta(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
||
| 1101 | print $sep; |
||
| 1102 | print '"'.$reflabel.'"'.$sep; |
||
| 1103 | print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
||
| 1104 | print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
||
| 1105 | print '"'.$journal.'"'.$sep; |
||
| 1106 | print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
||
| 1107 | print "\n"; |
||
| 1108 | } |
||
| 1109 | } |
||
| 1110 | } |
||
| 1111 | } |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | |||
| 1115 | /* |
||
| 1116 | * View |
||
| 1117 | */ |
||
| 1118 | |||
| 1119 | $form = new Form($db); |
||
| 1120 | |||
| 1121 | if (empty($action) || $action == 'view') { |
||
| 1122 | $invoicestatic = new Facture($db); |
||
| 1123 | $invoicesupplierstatic = new FactureFournisseur($db); |
||
| 1124 | $expensereportstatic = new ExpenseReport($db); |
||
| 1125 | $vatstatic = new Tva($db); |
||
| 1126 | $donationstatic = new Don($db); |
||
| 1127 | $loanstatic = new Loan($db); |
||
| 1128 | $salarystatic = new Salary($db); |
||
| 1129 | $variousstatic = new PaymentVarious($db); |
||
| 1130 | |||
| 1131 | $title = $langs->trans("GenerationOfAccountingEntries").' - '.$accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
||
| 1132 | |||
| 1133 | llxHeader('', dol_string_nohtmltag($title)); |
||
| 1134 | |||
| 1135 | $nom = $title; |
||
| 1136 | $builddate = dol_now(); |
||
| 1137 | //$description = $langs->trans("DescFinanceJournal") . '<br>'; |
||
| 1138 | $description = $langs->trans("DescJournalOnlyBindedVisible").'<br>'; |
||
| 1139 | |||
| 1140 | $listofchoices = array( |
||
| 1141 | 'notyet' => $langs->trans("NotYetInGeneralLedger"), |
||
| 1142 | 'already' => $langs->trans("AlreadyInGeneralLedger") |
||
| 1143 | ); |
||
| 1144 | $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
||
| 1145 | $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
||
| 1146 | |||
| 1147 | $varlink = 'id_journal='.$id_journal; |
||
| 1148 | $periodlink = ''; |
||
| 1149 | $exportlink = ''; |
||
| 1150 | |||
| 1151 | journalHead($nom, '', $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink); |
||
| 1152 | |||
| 1153 | $desc = ''; |
||
| 1154 | |||
| 1155 | if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
||
| 1156 | // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
||
| 1157 | // Fiscal period test |
||
| 1158 | $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity); |
||
| 1159 | $resql = $db->query($sql); |
||
| 1160 | if ($resql) { |
||
| 1161 | $obj = $db->fetch_object($resql); |
||
| 1162 | if ($obj->nb == 0) { |
||
| 1163 | print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined"); |
||
| 1164 | $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
||
| 1165 | $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc); |
||
| 1166 | print $desc; |
||
| 1167 | print '</div>'; |
||
| 1168 | } |
||
| 1169 | } else { |
||
| 1170 | dol_print_error($db); |
||
| 1171 | } |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | // Bank test |
||
| 1175 | $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."bank_account WHERE entity = ".((int) $conf->entity)." AND fk_accountancy_journal IS NULL AND clos=0"; |
||
| 1176 | $resql = $db->query($sql); |
||
| 1177 | if ($resql) { |
||
| 1178 | $obj = $db->fetch_object($resql); |
||
| 1179 | if ($obj->nb > 0) { |
||
| 1180 | print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount"); |
||
| 1181 | $desc = ' : '.$langs->trans("AccountancyAreaDescBank", 6, '{link}'); |
||
| 1182 | $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("BankAccounts").'</strong>', $desc); |
||
| 1183 | print $desc; |
||
| 1184 | print '</div>'; |
||
| 1185 | } |
||
| 1186 | } else { |
||
| 1187 | dol_print_error($db); |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | |||
| 1191 | // Button to write into Ledger |
||
| 1192 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
||
| 1193 | || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1' |
||
| 1194 | || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == "" || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1') { |
||
| 1195 | print($desc ? '' : '<br>').'<div class="warning">'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
||
| 1196 | $desc = ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
||
| 1197 | $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>', $desc); |
||
| 1198 | print $desc; |
||
| 1199 | print '</div>'; |
||
| 1200 | } |
||
| 1201 | |||
| 1202 | |||
| 1203 | print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
||
| 1204 | |||
| 1205 | if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
||
| 1206 | print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />'; |
||
| 1207 | } |
||
| 1208 | |||
| 1209 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
||
| 1210 | || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1') { |
||
| 1211 | print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />'; |
||
| 1212 | } else { |
||
| 1213 | if ($in_bookkeeping == 'notyet') { |
||
| 1214 | print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />'; |
||
| 1215 | } else { |
||
| 1216 | print '<a class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>'; |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | |||
| 1220 | print '</div>'; |
||
| 1221 | |||
| 1222 | // TODO Avoid using js. We can use a direct link with $param |
||
| 1223 | print ' |
||
| 1224 | <script type="text/javascript"> |
||
| 1225 | function launch_export() { |
||
| 1226 | console.log("Set value into form and submit"); |
||
| 1227 | $("div.fiche form input[name=\"action\"]").val("exportcsv"); |
||
| 1228 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 1229 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 1230 | } |
||
| 1231 | function writebookkeeping() { |
||
| 1232 | console.log("Set value into form and submit"); |
||
| 1233 | $("div.fiche form input[name=\"action\"]").val("writebookkeeping"); |
||
| 1234 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 1235 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 1236 | } |
||
| 1237 | </script>'; |
||
| 1238 | |||
| 1239 | /* |
||
| 1240 | * Show result array |
||
| 1241 | */ |
||
| 1242 | print '<br>'; |
||
| 1243 | |||
| 1244 | $i = 0; |
||
| 1245 | print '<div class="div-table-responsive">'; |
||
| 1246 | print '<table class="noborder centpercent">'; |
||
| 1247 | print '<tr class="liste_titre">'; |
||
| 1248 | print "<td>".$langs->trans("Date")."</td>"; |
||
| 1249 | print "<td>".$langs->trans("Piece").' ('.$langs->trans("ObjectsRef").")</td>"; |
||
| 1250 | print "<td>".$langs->trans("AccountAccounting")."</td>"; |
||
| 1251 | print "<td>".$langs->trans("SubledgerAccount")."</td>"; |
||
| 1252 | print "<td>".$langs->trans("LabelOperation")."</td>"; |
||
| 1253 | print '<td class="center">'.$langs->trans("PaymentMode")."</td>"; |
||
| 1254 | print '<td class="right">'.$langs->trans("AccountingDebit")."</td>"; |
||
| 1255 | print '<td class="right">'.$langs->trans("AccountingCredit")."</td>"; |
||
| 1256 | print "</tr>\n"; |
||
| 1257 | |||
| 1258 | $r = ''; |
||
| 1259 | |||
| 1260 | foreach ($tabpay as $key => $val) { // $key is rowid in llx_bank |
||
| 1261 | $date = dol_print_date($val["date"], 'day'); |
||
| 1262 | |||
| 1263 | $ref = getSourceDocRef($val, $tabtype[$key]); |
||
| 1264 | |||
| 1265 | // Bank |
||
| 1266 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 1267 | if ($mt) { |
||
| 1268 | $reflabel = ''; |
||
| 1269 | if (!empty($val['lib'])) { |
||
| 1270 | $reflabel .= $val['lib']." - "; |
||
| 1271 | } |
||
| 1272 | $reflabel .= $langs->trans("Bank").' '.$val['bank_account_ref']; |
||
| 1273 | if (!empty($val['soclib'])) { |
||
| 1274 | $reflabel .= " - ".$val['soclib']; |
||
| 1275 | } |
||
| 1276 | |||
| 1277 | //var_dump($tabpay[$key]); |
||
| 1278 | print '<!-- Bank bank.rowid='.$key.' type='.$tabpay[$key]['type'].' ref='.$tabpay[$key]['ref'].'-->'; |
||
| 1279 | print '<tr class="oddeven">'; |
||
| 1280 | |||
| 1281 | // Date |
||
| 1282 | print "<td>".$date."</td>"; |
||
| 1283 | |||
| 1284 | // Ref |
||
| 1285 | print "<td>".dol_escape_htmltag($ref)."</td>"; |
||
| 1286 | |||
| 1287 | // Ledger account |
||
| 1288 | $accounttoshow = length_accountg($k); |
||
| 1289 | if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
||
| 1290 | $accounttoshow = '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>'; |
||
| 1291 | } |
||
| 1292 | print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
||
| 1293 | print $accounttoshow; |
||
| 1294 | print "</td>"; |
||
| 1295 | |||
| 1296 | // Subledger account |
||
| 1297 | print '<td class="maxwidth300">'; |
||
| 1298 | /*$accounttoshow = length_accountg($k); |
||
| 1299 | if (empty($accounttoshow) || $accounttoshow == 'NotDefined') |
||
| 1300 | { |
||
| 1301 | print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>'; |
||
| 1302 | } |
||
| 1303 | else print $accounttoshow;*/ |
||
| 1304 | print "</td>"; |
||
| 1305 | |||
| 1306 | // Label operation |
||
| 1307 | print '<td>'; |
||
| 1308 | print $reflabel; // This is already html escaped content |
||
| 1309 | print "</td>"; |
||
| 1310 | |||
| 1311 | print '<td class="center">'.$val["type_payment"]."</td>"; |
||
| 1312 | print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
||
| 1313 | print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
||
| 1314 | print "</tr>"; |
||
| 1315 | |||
| 1316 | $i++; |
||
| 1317 | } |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | // Third party |
||
| 1321 | if (is_array($tabtp[$key])) { |
||
| 1322 | foreach ($tabtp[$key] as $k => $mt) { |
||
| 1323 | if ($mt) { |
||
| 1324 | $reflabel = ''; |
||
| 1325 | if (!empty($val['lib'])) { |
||
| 1326 | $reflabel .= $val['lib'].($val['soclib'] ? " - " : ""); |
||
| 1327 | } |
||
| 1328 | if ($tabtype[$key] == 'banktransfert') { |
||
| 1329 | $reflabel .= $langs->trans('TransitionalAccount').' '.$account_transfer; |
||
| 1330 | } else { |
||
| 1331 | $reflabel .= $val['soclib']; |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | print '<!-- Thirdparty bank.rowid='.$key.' -->'; |
||
| 1335 | print '<tr class="oddeven">'; |
||
| 1336 | |||
| 1337 | // Date |
||
| 1338 | print "<td>".$date."</td>"; |
||
| 1339 | |||
| 1340 | // Ref |
||
| 1341 | print "<td>".dol_escape_htmltag($ref)."</td>"; |
||
| 1342 | |||
| 1343 | // Ledger account |
||
| 1344 | $account_ledger = $k; |
||
| 1345 | // Try to force general ledger account depending on type |
||
| 1346 | if ($tabtype[$key] == 'payment') { |
||
| 1347 | $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
||
| 1348 | } |
||
| 1349 | if ($tabtype[$key] == 'payment_supplier') { |
||
| 1350 | $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
||
| 1351 | } |
||
| 1352 | if ($tabtype[$key] == 'payment_expensereport') { |
||
| 1353 | $account_ledger = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
||
| 1354 | } |
||
| 1355 | if ($tabtype[$key] == 'payment_salary') { |
||
| 1356 | $account_ledger = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
||
| 1357 | } |
||
| 1358 | if ($tabtype[$key] == 'payment_vat') { |
||
| 1359 | $account_ledger = getDolGlobalString('ACCOUNTING_VAT_PAY_ACCOUNT'); |
||
| 1360 | } |
||
| 1361 | if ($tabtype[$key] == 'member') { |
||
| 1362 | $account_ledger = getDolGlobalString('ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT'); |
||
| 1363 | } |
||
| 1364 | if ($tabtype[$key] == 'payment_various') { |
||
| 1365 | $account_ledger = $tabpay[$key]["account_various"]; |
||
| 1366 | } |
||
| 1367 | $accounttoshow = length_accountg($account_ledger); |
||
| 1368 | if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
||
| 1369 | if ($tabtype[$key] == 'unknown') { |
||
| 1370 | // We will accept writing, but into a waiting account |
||
| 1371 | if (!getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') || getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') == '-1') { |
||
| 1372 | $accounttoshow = '<span class="error small">'.$langs->trans('UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking').'</span>'; |
||
| 1373 | } else { |
||
| 1374 | $accounttoshow = '<span class="warning small">'.$langs->trans('UnknownAccountForThirdparty', length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'))).'</span>'; // We will use a waiting account |
||
| 1375 | } |
||
| 1376 | } else { |
||
| 1377 | // We will refuse writing |
||
| 1378 | $errorstring = 'UnknownAccountForThirdpartyBlocking'; |
||
| 1379 | if ($tabtype[$key] == 'payment') { |
||
| 1380 | $errorstring = 'MainAccountForCustomersNotDefined'; |
||
| 1381 | } |
||
| 1382 | if ($tabtype[$key] == 'payment_supplier') { |
||
| 1383 | $errorstring = 'MainAccountForSuppliersNotDefined'; |
||
| 1384 | } |
||
| 1385 | if ($tabtype[$key] == 'payment_expensereport') { |
||
| 1386 | $errorstring = 'MainAccountForUsersNotDefined'; |
||
| 1387 | } |
||
| 1388 | if ($tabtype[$key] == 'payment_salary') { |
||
| 1389 | $errorstring = 'MainAccountForUsersNotDefined'; |
||
| 1390 | } |
||
| 1391 | if ($tabtype[$key] == 'payment_vat') { |
||
| 1392 | $errorstring = 'MainAccountForVatPaymentNotDefined'; |
||
| 1393 | } |
||
| 1394 | if ($tabtype[$key] == 'member') { |
||
| 1395 | $errorstring = 'MainAccountForSubscriptionPaymentNotDefined'; |
||
| 1396 | } |
||
| 1397 | $accounttoshow = '<span class="error small">'.$langs->trans($errorstring).'</span>'; |
||
| 1398 | } |
||
| 1399 | } |
||
| 1400 | print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
||
| 1401 | print $accounttoshow; // This is a HTML string |
||
| 1402 | print "</td>"; |
||
| 1403 | |||
| 1404 | // Subledger account |
||
| 1405 | $accounttoshowsubledger = ''; |
||
| 1406 | if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary', 'payment_various'))) { // Type of payments that uses a subledger |
||
| 1407 | $accounttoshowsubledger = length_accounta($k); |
||
| 1408 | if ($accounttoshow != $accounttoshowsubledger) { |
||
| 1409 | if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined') { |
||
| 1410 | //var_dump($tabpay[$key]); |
||
| 1411 | //var_dump($tabtype[$key]); |
||
| 1412 | //var_dump($tabbq[$key]); |
||
| 1413 | //print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>'; |
||
| 1414 | if (!empty($tabcompany[$key]['code_compta'])) { |
||
| 1415 | if (in_array($tabtype[$key], array('payment_various', 'payment_salary'))) { |
||
| 1416 | // For such case, if subledger is not defined, we won't use subledger accounts. |
||
| 1417 | $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownSubledgerIgnored").'</span>'; |
||
| 1418 | } else { |
||
| 1419 | $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown", $tabcompany[$key]['code_compta']).'</span>'; |
||
| 1420 | } |
||
| 1421 | } else { |
||
| 1422 | $accounttoshowsubledger = '<span class="error small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking").'</span>'; |
||
| 1423 | } |
||
| 1424 | } |
||
| 1425 | } else { |
||
| 1426 | $accounttoshowsubledger = ''; |
||
| 1427 | } |
||
| 1428 | } |
||
| 1429 | print '<td class="maxwidth300">'; |
||
| 1430 | print $accounttoshowsubledger; // This is a html string |
||
| 1431 | print "</td>"; |
||
| 1432 | |||
| 1433 | print "<td>".$reflabel."</td>"; |
||
| 1434 | |||
| 1435 | print '<td class="center">'.$val["type_payment"]."</td>"; |
||
| 1436 | |||
| 1437 | print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
||
| 1438 | |||
| 1439 | print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
||
| 1440 | |||
| 1441 | print "</tr>"; |
||
| 1442 | |||
| 1443 | $i++; |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | } else { // Waiting account |
||
| 1447 | foreach ($tabbq[$key] as $k => $mt) { |
||
| 1448 | if ($mt) { |
||
| 1449 | $reflabel = ''; |
||
| 1450 | if (!empty($val['lib'])) { |
||
| 1451 | $reflabel .= $val['lib']." - "; |
||
| 1452 | } |
||
| 1453 | $reflabel .= 'WaitingAccount'; |
||
| 1454 | |||
| 1455 | print '<!-- Wait bank.rowid='.$key.' -->'; |
||
| 1456 | print '<tr class="oddeven">'; |
||
| 1457 | print "<td>".$date."</td>"; |
||
| 1458 | print "<td>".$ref."</td>"; |
||
| 1459 | // Ledger account |
||
| 1460 | print "<td>"; |
||
| 1461 | /*if (empty($accounttoshow) || $accounttoshow == 'NotDefined') |
||
| 1462 | { |
||
| 1463 | print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>'; |
||
| 1464 | } |
||
| 1465 | else */ |
||
| 1466 | print length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')); |
||
| 1467 | print "</td>"; |
||
| 1468 | // Subledger account |
||
| 1469 | print "<td>"; |
||
| 1470 | print "</td>"; |
||
| 1471 | print "<td>".dol_escape_htmltag($reflabel)."</td>"; |
||
| 1472 | print '<td class="center">'.$val["type_payment"]."</td>"; |
||
| 1473 | print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
||
| 1474 | print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
||
| 1475 | print "</tr>"; |
||
| 1476 | |||
| 1477 | $i++; |
||
| 1478 | } |
||
| 1479 | } |
||
| 1480 | } |
||
| 1481 | } |
||
| 1482 | |||
| 1483 | if (!$i) { |
||
| 1484 | $colspan = 8; |
||
| 1485 | print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>'; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | print "</table>"; |
||
| 1489 | print '</div>'; |
||
| 1490 | |||
| 1491 | llxFooter(); |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | $db->close(); |
||
| 1495 | |||
| 1496 | |||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * Return source for doc_ref of a bank transaction |
||
| 1500 | * |
||
| 1501 | * @param array $val Array of val |
||
| 1502 | * @param string $typerecord Type of record ('payment', 'payment_supplier', 'payment_expensereport', 'payment_vat', ...) |
||
| 1503 | * @return string A string label to describe a record into llx_bank_url |
||
| 1504 | */ |
||
| 1505 | function getSourceDocRef($val, $typerecord) |
||
| 1506 | { |
||
| 1507 | global $db, $langs; |
||
| 1508 | |||
| 1509 | // Defined the docref into $ref (We start with $val['ref'] by default and we complete according to other data) |
||
| 1510 | // WE MUST HAVE SAME REF FOR ALL LINES WE WILL RECORD INTO THE BOOKKEEPING |
||
| 1511 | $ref = $val['ref']; |
||
| 1512 | if ($ref == '(SupplierInvoicePayment)' || $ref == '(SupplierInvoicePaymentBack)') { |
||
| 1513 | $ref = $langs->transnoentitiesnoconv('Supplier'); |
||
| 1514 | } |
||
| 1515 | if ($ref == '(CustomerInvoicePayment)' || $ref == '(CustomerInvoicePaymentBack)') { |
||
| 1516 | $ref = $langs->transnoentitiesnoconv('Customer'); |
||
| 1517 | } |
||
| 1518 | if ($ref == '(SocialContributionPayment)') { |
||
| 1519 | $ref = $langs->transnoentitiesnoconv('SocialContribution'); |
||
| 1520 | } |
||
| 1521 | if ($ref == '(DonationPayment)') { |
||
| 1522 | $ref = $langs->transnoentitiesnoconv('Donation'); |
||
| 1523 | } |
||
| 1524 | if ($ref == '(SubscriptionPayment)') { |
||
| 1525 | $ref = $langs->transnoentitiesnoconv('Subscription'); |
||
| 1526 | } |
||
| 1527 | if ($ref == '(ExpenseReportPayment)') { |
||
| 1528 | $ref = $langs->transnoentitiesnoconv('Employee'); |
||
| 1529 | } |
||
| 1530 | if ($ref == '(LoanPayment)') { |
||
| 1531 | $ref = $langs->transnoentitiesnoconv('Loan'); |
||
| 1532 | } |
||
| 1533 | if ($ref == '(payment_salary)') { |
||
| 1534 | $ref = $langs->transnoentitiesnoconv('Employee'); |
||
| 1535 | } |
||
| 1536 | |||
| 1537 | $sqlmid = ''; |
||
| 1538 | if ($typerecord == 'payment') { |
||
| 1539 | if (getDolGlobalInt('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { |
||
| 1540 | $sqlmid = "SELECT payfac.fk_facture as id, ".$db->ifsql('f1.rowid IS NULL', 'f.ref', 'f1.ref')." as ref"; |
||
| 1541 | $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
||
| 1542 | $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
||
| 1543 | $sqlmid .= " LEFT JOIN ".$db->prefix()."societe_remise_except as sre ON sre.fk_facture_source = payfac.fk_facture"; |
||
| 1544 | $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f1 ON f1.rowid = sre.fk_facture"; |
||
| 1545 | $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
||
| 1546 | } else { |
||
| 1547 | $sqlmid = "SELECT payfac.fk_facture as id, f.ref as ref"; |
||
| 1548 | $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
||
| 1549 | $sqlmid .= " INNER JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
||
| 1550 | $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
||
| 1551 | } |
||
| 1552 | $ref = $langs->transnoentitiesnoconv("Invoice"); |
||
| 1553 | } elseif ($typerecord == 'payment_supplier') { |
||
| 1554 | $sqlmid = 'SELECT payfac.fk_facturefourn as id, f.ref'; |
||
| 1555 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfac, ".MAIN_DB_PREFIX."facture_fourn as f"; |
||
| 1556 | $sqlmid .= " WHERE payfac.fk_facturefourn = f.rowid AND payfac.fk_paiementfourn=".((int) $val["paymentsupplierid"]); |
||
| 1557 | $ref = $langs->transnoentitiesnoconv("SupplierInvoice"); |
||
| 1558 | } elseif ($typerecord == 'payment_expensereport') { |
||
| 1559 | $sqlmid = 'SELECT e.rowid as id, e.ref'; |
||
| 1560 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as pe, ".MAIN_DB_PREFIX."expensereport as e"; |
||
| 1561 | $sqlmid .= " WHERE pe.rowid=".((int) $val["paymentexpensereport"])." AND pe.fk_expensereport = e.rowid"; |
||
| 1562 | $ref = $langs->transnoentitiesnoconv("ExpenseReport"); |
||
| 1563 | } elseif ($typerecord == 'payment_salary') { |
||
| 1564 | $sqlmid = 'SELECT s.rowid as ref'; |
||
| 1565 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_salary as s"; |
||
| 1566 | $sqlmid .= " WHERE s.rowid=".((int) $val["paymentsalid"]); |
||
| 1567 | $ref = $langs->transnoentitiesnoconv("SalaryPayment"); |
||
| 1568 | } elseif ($typerecord == 'sc') { |
||
| 1569 | $sqlmid = 'SELECT sc.rowid as ref'; |
||
| 1570 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementcharge as sc"; |
||
| 1571 | $sqlmid .= " WHERE sc.rowid=".((int) $val["paymentscid"]); |
||
| 1572 | $ref = $langs->transnoentitiesnoconv("SocialContribution"); |
||
| 1573 | } elseif ($typerecord == 'payment_vat') { |
||
| 1574 | $sqlmid = 'SELECT v.rowid as ref'; |
||
| 1575 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."tva as v"; |
||
| 1576 | $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvatid"]); |
||
| 1577 | $ref = $langs->transnoentitiesnoconv("PaymentVat"); |
||
| 1578 | } elseif ($typerecord == 'payment_donation') { |
||
| 1579 | $sqlmid = 'SELECT payd.fk_donation as ref'; |
||
| 1580 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_donation as payd"; |
||
| 1581 | $sqlmid .= " WHERE payd.fk_donation=".((int) $val["paymentdonationid"]); |
||
| 1582 | $ref = $langs->transnoentitiesnoconv("Donation"); |
||
| 1583 | } elseif ($typerecord == 'payment_loan') { |
||
| 1584 | $sqlmid = 'SELECT l.rowid as ref'; |
||
| 1585 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_loan as l"; |
||
| 1586 | $sqlmid .= " WHERE l.rowid=".((int) $val["paymentloanid"]); |
||
| 1587 | $ref = $langs->transnoentitiesnoconv("LoanPayment"); |
||
| 1588 | } elseif ($typerecord == 'payment_various') { |
||
| 1589 | $sqlmid = 'SELECT v.rowid as ref'; |
||
| 1590 | $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_various as v"; |
||
| 1591 | $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvariousid"]); |
||
| 1592 | $ref = $langs->transnoentitiesnoconv("VariousPayment"); |
||
| 1593 | } |
||
| 1594 | // Add warning |
||
| 1595 | if (empty($sqlmid)) { |
||
| 1596 | dol_syslog("Found a typerecord=".$typerecord." not supported", LOG_WARNING); |
||
| 1597 | } |
||
| 1598 | |||
| 1599 | if ($sqlmid) { |
||
| 1600 | dol_syslog("accountancy/journal/bankjournal.php::sqlmid=".$sqlmid, LOG_DEBUG); |
||
| 1601 | $resultmid = $db->query($sqlmid); |
||
| 1602 | if ($resultmid) { |
||
| 1603 | while ($objmid = $db->fetch_object($resultmid)) { |
||
| 1604 | $ref .= ' '.$objmid->ref; |
||
| 1605 | } |
||
| 1606 | } else { |
||
| 1607 | dol_print_error($db); |
||
| 1608 | } |
||
| 1609 | } |
||
| 1610 | |||
| 1611 | $ref = dol_trunc($langs->transnoentitiesnoconv("BankId").' '.$val['fk_bank'].' - '.$ref, 295); // 295 + 3 dots (...) is < than max size of 300 |
||
| 1612 | return $ref; |
||
| 1613 | } |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * \file htdocs/accountancy/journal/expensereportsjournal.php |
||
| 1618 | * \ingroup Accountancy (Double entries) |
||
| 1619 | * \brief Page with expense reports journal |
||
| 1620 | */ |
||
| 1621 | public function expensereportjournal() |
||
| 1622 | { |
||
| 1623 | global $conf; |
||
| 1624 | global $db; |
||
| 1625 | global $user; |
||
| 1626 | global $hookmanager; |
||
| 1627 | global $user; |
||
| 1628 | global $menumanager; |
||
| 1629 | global $langs; |
||
| 1630 | global $mysoc; |
||
| 1631 | |||
| 1632 | // Load translation files required by the page |
||
| 1633 | $langs->loadLangs(array("commercial", "compta", "bills", "other", "accountancy", "trips", "errors")); |
||
| 1634 | |||
| 1635 | $id_journal = GETPOSTINT('id_journal'); |
||
| 1636 | $action = GETPOST('action', 'aZ09'); |
||
| 1637 | |||
| 1638 | $date_startmonth = GETPOST('date_startmonth'); |
||
| 1639 | $date_startday = GETPOST('date_startday'); |
||
| 1640 | $date_startyear = GETPOST('date_startyear'); |
||
| 1641 | $date_endmonth = GETPOST('date_endmonth'); |
||
| 1642 | $date_endday = GETPOST('date_endday'); |
||
| 1643 | $date_endyear = GETPOST('date_endyear'); |
||
| 1644 | $in_bookkeeping = GETPOST('in_bookkeeping'); |
||
| 1645 | if ($in_bookkeeping == '') { |
||
| 1646 | $in_bookkeeping = 'notyet'; |
||
| 1647 | } |
||
| 1648 | |||
| 1649 | $now = dol_now(); |
||
| 1650 | |||
| 1651 | $hookmanager->initHooks(array('expensereportsjournal')); |
||
| 1652 | $parameters = array(); |
||
| 1653 | |||
| 1654 | // Security check |
||
| 1655 | if (!isModEnabled('accounting')) { |
||
| 1656 | accessforbidden(); |
||
| 1657 | } |
||
| 1658 | if ($user->socid > 0) { |
||
| 1659 | accessforbidden(); |
||
| 1660 | } |
||
| 1661 | if (!$user->hasRight('accounting', 'mouvements', 'lire')) { |
||
| 1662 | accessforbidden(); |
||
| 1663 | } |
||
| 1664 | |||
| 1665 | $error = 0; |
||
| 1666 | $errorforinvoice = array(); |
||
| 1667 | |||
| 1668 | |||
| 1669 | /* |
||
| 1670 | * Actions |
||
| 1671 | */ |
||
| 1672 | |||
| 1673 | $accountingaccount = new AccountingAccount($db); |
||
| 1674 | |||
| 1675 | // Get information of journal |
||
| 1676 | $accountingjournalstatic = new AccountingJournal($db); |
||
| 1677 | $accountingjournalstatic->fetch($id_journal); |
||
| 1678 | $journal = $accountingjournalstatic->code; |
||
| 1679 | $journal_label = $accountingjournalstatic->label; |
||
| 1680 | |||
| 1681 | $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); |
||
| 1682 | $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); |
||
| 1683 | |||
| 1684 | if (empty($date_startmonth)) { |
||
| 1685 | // Period by default on transfer |
||
| 1686 | $dates = getDefaultDatesForTransfer(); |
||
| 1687 | $date_start = $dates['date_start']; |
||
| 1688 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 1689 | $pastmonth = $dates['pastmonth']; |
||
| 1690 | } |
||
| 1691 | if (empty($date_endmonth)) { |
||
| 1692 | // Period by default on transfer |
||
| 1693 | $dates = getDefaultDatesForTransfer(); |
||
| 1694 | $date_end = $dates['date_end']; |
||
| 1695 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 1696 | $pastmonth = $dates['pastmonth']; |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form |
||
| 1700 | $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); |
||
| 1701 | $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); |
||
| 1702 | } |
||
| 1703 | |||
| 1704 | $sql = "SELECT er.rowid, er.ref, er.date_debut as de,"; |
||
| 1705 | $sql .= " erd.rowid as erdid, erd.comments, erd.total_ht, erd.total_tva, erd.total_localtax1, erd.total_localtax2, erd.tva_tx, erd.total_ttc, erd.fk_code_ventilation, erd.vat_src_code, "; |
||
| 1706 | $sql .= " u.rowid as uid, u.firstname, u.lastname, u.accountancy_code as user_accountancy_account,"; |
||
| 1707 | $sql .= " f.accountancy_code, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte"; |
||
| 1708 | $parameters = array(); |
||
| 1709 | $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook |
||
| 1710 | $sql .= $hookmanager->resPrint; |
||
| 1711 | $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; |
||
| 1712 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_fees as f ON f.id = erd.fk_c_type_fees"; |
||
| 1713 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation"; |
||
| 1714 | $sql .= " JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport"; |
||
| 1715 | $sql .= " JOIN " . MAIN_DB_PREFIX . "user as u ON u.rowid = er.fk_user_author"; |
||
| 1716 | $parameters = array(); |
||
| 1717 | $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters); // Note that $action and $object may have been modified by hook |
||
| 1718 | $sql .= $hookmanager->resPrint; |
||
| 1719 | $sql .= " WHERE er.fk_statut > 0"; |
||
| 1720 | $sql .= " AND erd.fk_code_ventilation > 0"; |
||
| 1721 | $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy |
||
| 1722 | if ($date_start && $date_end) { |
||
| 1723 | $sql .= " AND er.date_debut >= '" . $db->idate($date_start) . "' AND er.date_debut <= '" . $db->idate($date_end) . "'"; |
||
| 1724 | } |
||
| 1725 | // Define begin binding date |
||
| 1726 | if (getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) { |
||
| 1727 | $sql .= " AND er.date_debut >= '" . $db->idate(getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) . "'"; |
||
| 1728 | } |
||
| 1729 | // Already in bookkeeping or not |
||
| 1730 | if ($in_bookkeeping == 'already') { |
||
| 1731 | $sql .= " AND er.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')"; |
||
| 1732 | } |
||
| 1733 | if ($in_bookkeeping == 'notyet') { |
||
| 1734 | $sql .= " AND er.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='expense_report')"; |
||
| 1735 | } |
||
| 1736 | $parameters = array(); |
||
| 1737 | $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook |
||
| 1738 | $sql .= $hookmanager->resPrint; |
||
| 1739 | $sql .= " ORDER BY er.date_debut"; |
||
| 1740 | |||
| 1741 | dol_syslog('accountancy/journal/expensereportsjournal.php', LOG_DEBUG); |
||
| 1742 | $result = $db->query($sql); |
||
| 1743 | if ($result) { |
||
| 1744 | $taber = array(); |
||
| 1745 | $tabht = array(); |
||
| 1746 | $tabtva = array(); |
||
| 1747 | $def_tva = array(); |
||
| 1748 | $tabttc = array(); |
||
| 1749 | $tablocaltax1 = array(); |
||
| 1750 | $tablocaltax2 = array(); |
||
| 1751 | $tabuser = array(); |
||
| 1752 | |||
| 1753 | $num = $db->num_rows($result); |
||
| 1754 | |||
| 1755 | // Variables |
||
| 1756 | $account_salary = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT', 'NotDefined'); |
||
| 1757 | $account_vat = getDolGlobalString('ACCOUNTING_VAT_BUY_ACCOUNT', 'NotDefined'); |
||
| 1758 | |||
| 1759 | $i = 0; |
||
| 1760 | while ($i < $num) { |
||
| 1761 | $obj = $db->fetch_object($result); |
||
| 1762 | |||
| 1763 | // Controls |
||
| 1764 | $compta_user = (!empty($obj->user_accountancy_account)) ? $obj->user_accountancy_account : $account_salary; |
||
| 1765 | $compta_fees = $obj->compte; |
||
| 1766 | |||
| 1767 | $vatdata = getTaxesFromId($obj->tva_tx . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : ''), $mysoc, $mysoc, 0); |
||
| 1768 | $compta_tva = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $account_vat); |
||
| 1769 | $compta_localtax1 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); |
||
| 1770 | $compta_localtax2 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); |
||
| 1771 | |||
| 1772 | // Define array to display all VAT rates that use this accounting account $compta_tva |
||
| 1773 | if (price2num($obj->tva_tx) || !empty($obj->vat_src_code)) { |
||
| 1774 | $def_tva[$obj->rowid][$compta_tva][vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')] = (vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')); |
||
| 1775 | } |
||
| 1776 | |||
| 1777 | $taber[$obj->rowid]["date"] = $db->jdate($obj->de); |
||
| 1778 | $taber[$obj->rowid]["ref"] = $obj->ref; |
||
| 1779 | $taber[$obj->rowid]["comments"] = $obj->comments; |
||
| 1780 | $taber[$obj->rowid]["fk_expensereportdet"] = $obj->erdid; |
||
| 1781 | |||
| 1782 | // Avoid warnings |
||
| 1783 | if (!isset($tabttc[$obj->rowid][$compta_user])) { |
||
| 1784 | $tabttc[$obj->rowid][$compta_user] = 0; |
||
| 1785 | } |
||
| 1786 | if (!isset($tabht[$obj->rowid][$compta_fees])) { |
||
| 1787 | $tabht[$obj->rowid][$compta_fees] = 0; |
||
| 1788 | } |
||
| 1789 | if (!isset($tabtva[$obj->rowid][$compta_tva])) { |
||
| 1790 | $tabtva[$obj->rowid][$compta_tva] = 0; |
||
| 1791 | } |
||
| 1792 | if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) { |
||
| 1793 | $tablocaltax1[$obj->rowid][$compta_localtax1] = 0; |
||
| 1794 | } |
||
| 1795 | if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) { |
||
| 1796 | $tablocaltax2[$obj->rowid][$compta_localtax2] = 0; |
||
| 1797 | } |
||
| 1798 | |||
| 1799 | $tabttc[$obj->rowid][$compta_user] += $obj->total_ttc; |
||
| 1800 | $tabht[$obj->rowid][$compta_fees] += $obj->total_ht; |
||
| 1801 | $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva; |
||
| 1802 | $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1; |
||
| 1803 | $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2; |
||
| 1804 | $tabuser[$obj->rowid] = array( |
||
| 1805 | 'id' => $obj->uid, |
||
| 1806 | 'name' => dolGetFirstLastname($obj->firstname, $obj->lastname), |
||
| 1807 | 'user_accountancy_code' => $obj->user_accountancy_account |
||
| 1808 | ); |
||
| 1809 | |||
| 1810 | $i++; |
||
| 1811 | } |
||
| 1812 | } else { |
||
| 1813 | dol_print_error($db); |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | // Load all unbound lines |
||
| 1817 | $sql = "SELECT fk_expensereport, COUNT(erd.rowid) as nb"; |
||
| 1818 | $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; |
||
| 1819 | $sql .= " WHERE erd.fk_code_ventilation <= 0"; |
||
| 1820 | $sql .= " AND erd.total_ttc <> 0"; |
||
| 1821 | $sql .= " AND fk_expensereport IN (" . $db->sanitize(implode(",", array_keys($taber))) . ")"; |
||
| 1822 | $sql .= " GROUP BY fk_expensereport"; |
||
| 1823 | $resql = $db->query($sql); |
||
| 1824 | |||
| 1825 | $num = $db->num_rows($resql); |
||
| 1826 | $i = 0; |
||
| 1827 | while ($i < $num) { |
||
| 1828 | $obj = $db->fetch_object($resql); |
||
| 1829 | if ($obj->nb > 0) { |
||
| 1830 | $errorforinvoice[$obj->fk_expensereport] = 'somelinesarenotbound'; |
||
| 1831 | } |
||
| 1832 | $i++; |
||
| 1833 | } |
||
| 1834 | |||
| 1835 | // Bookkeeping Write |
||
| 1836 | if ($action == 'writebookkeeping' && !$error) { |
||
| 1837 | $now = dol_now(); |
||
| 1838 | $error = 0; |
||
| 1839 | |||
| 1840 | $accountingaccountexpense = new AccountingAccount($db); |
||
| 1841 | $accountingaccountexpense->fetch(null, getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'), true); |
||
| 1842 | |||
| 1843 | foreach ($taber as $key => $val) { // Loop on each expense report |
||
| 1844 | $errorforline = 0; |
||
| 1845 | |||
| 1846 | $totalcredit = 0; |
||
| 1847 | $totaldebit = 0; |
||
| 1848 | |||
| 1849 | $db->begin(); |
||
| 1850 | |||
| 1851 | // Error if some lines are not binded/ready to be journalized |
||
| 1852 | if (!empty($errorforinvoice[$key]) && $errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 1853 | $error++; |
||
| 1854 | $errorforline++; |
||
| 1855 | setEventMessages($langs->trans('ErrorInvoiceContainsLinesNotYetBounded', $val['ref']), null, 'errors'); |
||
| 1856 | } |
||
| 1857 | |||
| 1858 | // Thirdparty |
||
| 1859 | if (!$errorforline) { |
||
| 1860 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 1861 | if ($mt) { |
||
| 1862 | $bookkeeping = new BookKeeping($db); |
||
| 1863 | $bookkeeping->doc_date = $val["date"]; |
||
| 1864 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 1865 | $bookkeeping->date_creation = $now; |
||
| 1866 | $bookkeeping->doc_type = 'expense_report'; |
||
| 1867 | $bookkeeping->fk_doc = $key; |
||
| 1868 | $bookkeeping->fk_docdet = $val["fk_expensereportdet"]; |
||
| 1869 | |||
| 1870 | $bookkeeping->subledger_account = $tabuser[$key]['user_accountancy_code']; |
||
| 1871 | $bookkeeping->subledger_label = $tabuser[$key]['name']; |
||
| 1872 | |||
| 1873 | $bookkeeping->numero_compte = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
||
| 1874 | $bookkeeping->label_compte = $accountingaccountexpense->label; |
||
| 1875 | |||
| 1876 | $bookkeeping->label_operation = $tabuser[$key]['name']; |
||
| 1877 | $bookkeeping->montant = $mt; |
||
| 1878 | $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D'; |
||
| 1879 | $bookkeeping->debit = ($mt <= 0) ? -$mt : 0; |
||
| 1880 | $bookkeeping->credit = ($mt > 0) ? $mt : 0; |
||
| 1881 | $bookkeeping->code_journal = $journal; |
||
| 1882 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 1883 | $bookkeeping->fk_user_author = $user->id; |
||
| 1884 | $bookkeeping->entity = $conf->entity; |
||
| 1885 | |||
| 1886 | $totaldebit += $bookkeeping->debit; |
||
| 1887 | $totalcredit += $bookkeeping->credit; |
||
| 1888 | |||
| 1889 | $result = $bookkeeping->create($user); |
||
| 1890 | if ($result < 0) { |
||
| 1891 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 1892 | $error++; |
||
| 1893 | $errorforline++; |
||
| 1894 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 1895 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 1896 | } else { |
||
| 1897 | $error++; |
||
| 1898 | $errorforline++; |
||
| 1899 | $errorforinvoice[$key] = 'other'; |
||
| 1900 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 1901 | } |
||
| 1902 | } |
||
| 1903 | } |
||
| 1904 | } |
||
| 1905 | } |
||
| 1906 | |||
| 1907 | // Fees |
||
| 1908 | if (!$errorforline) { |
||
| 1909 | foreach ($tabht[$key] as $k => $mt) { |
||
| 1910 | if ($mt) { |
||
| 1911 | // get compte id and label |
||
| 1912 | if ($accountingaccount->fetch(null, $k, true)) { |
||
| 1913 | $bookkeeping = new BookKeeping($db); |
||
| 1914 | $bookkeeping->doc_date = $val["date"]; |
||
| 1915 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 1916 | $bookkeeping->date_creation = $now; |
||
| 1917 | $bookkeeping->doc_type = 'expense_report'; |
||
| 1918 | $bookkeeping->fk_doc = $key; |
||
| 1919 | $bookkeeping->fk_docdet = $val["fk_expensereportdet"]; |
||
| 1920 | |||
| 1921 | $bookkeeping->subledger_account = ''; |
||
| 1922 | $bookkeeping->subledger_label = ''; |
||
| 1923 | |||
| 1924 | $bookkeeping->numero_compte = $k; |
||
| 1925 | $bookkeeping->label_compte = $accountingaccount->label; |
||
| 1926 | |||
| 1927 | $bookkeeping->label_operation = $accountingaccount->label; |
||
| 1928 | $bookkeeping->montant = $mt; |
||
| 1929 | $bookkeeping->sens = ($mt < 0) ? 'C' : 'D'; |
||
| 1930 | $bookkeeping->debit = ($mt > 0) ? $mt : 0; |
||
| 1931 | $bookkeeping->credit = ($mt <= 0) ? -$mt : 0; |
||
| 1932 | $bookkeeping->code_journal = $journal; |
||
| 1933 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 1934 | $bookkeeping->fk_user_author = $user->id; |
||
| 1935 | $bookkeeping->entity = $conf->entity; |
||
| 1936 | |||
| 1937 | $totaldebit += $bookkeeping->debit; |
||
| 1938 | $totalcredit += $bookkeeping->credit; |
||
| 1939 | |||
| 1940 | $result = $bookkeeping->create($user); |
||
| 1941 | if ($result < 0) { |
||
| 1942 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 1943 | $error++; |
||
| 1944 | $errorforline++; |
||
| 1945 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 1946 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 1947 | } else { |
||
| 1948 | $error++; |
||
| 1949 | $errorforline++; |
||
| 1950 | $errorforinvoice[$key] = 'other'; |
||
| 1951 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 1952 | } |
||
| 1953 | } |
||
| 1954 | } |
||
| 1955 | } |
||
| 1956 | } |
||
| 1957 | } |
||
| 1958 | |||
| 1959 | // VAT |
||
| 1960 | if (!$errorforline) { |
||
| 1961 | $listoftax = array(0, 1, 2); |
||
| 1962 | foreach ($listoftax as $numtax) { |
||
| 1963 | $arrayofvat = $tabtva; |
||
| 1964 | if ($numtax == 1) { |
||
| 1965 | $arrayofvat = $tablocaltax1; |
||
| 1966 | } |
||
| 1967 | if ($numtax == 2) { |
||
| 1968 | $arrayofvat = $tablocaltax2; |
||
| 1969 | } |
||
| 1970 | |||
| 1971 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 1972 | if ($mt) { |
||
| 1973 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache for label |
||
| 1974 | $account_label = $accountingaccount->label; |
||
| 1975 | |||
| 1976 | // get compte id and label |
||
| 1977 | $bookkeeping = new BookKeeping($db); |
||
| 1978 | $bookkeeping->doc_date = $val["date"]; |
||
| 1979 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 1980 | $bookkeeping->date_creation = $now; |
||
| 1981 | $bookkeeping->doc_type = 'expense_report'; |
||
| 1982 | $bookkeeping->fk_doc = $key; |
||
| 1983 | $bookkeeping->fk_docdet = $val["fk_expensereportdet"]; |
||
| 1984 | |||
| 1985 | $bookkeeping->subledger_account = ''; |
||
| 1986 | $bookkeeping->subledger_label = ''; |
||
| 1987 | |||
| 1988 | $bookkeeping->numero_compte = $k; |
||
| 1989 | $bookkeeping->label_compte = $account_label; |
||
| 1990 | |||
| 1991 | $bookkeeping->label_operation = $langs->trans("VAT") . ' ' . implode(', ', $def_tva[$key][$k]) . ' %'; |
||
| 1992 | $bookkeeping->montant = $mt; |
||
| 1993 | $bookkeeping->sens = ($mt < 0) ? 'C' : 'D'; |
||
| 1994 | $bookkeeping->debit = ($mt > 0) ? $mt : 0; |
||
| 1995 | $bookkeeping->credit = ($mt <= 0) ? -$mt : 0; |
||
| 1996 | $bookkeeping->code_journal = $journal; |
||
| 1997 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 1998 | $bookkeeping->fk_user_author = $user->id; |
||
| 1999 | $bookkeeping->entity = $conf->entity; |
||
| 2000 | |||
| 2001 | $totaldebit += $bookkeeping->debit; |
||
| 2002 | $totalcredit += $bookkeeping->credit; |
||
| 2003 | |||
| 2004 | $result = $bookkeeping->create($user); |
||
| 2005 | if ($result < 0) { |
||
| 2006 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 2007 | $error++; |
||
| 2008 | $errorforline++; |
||
| 2009 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 2010 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 2011 | } else { |
||
| 2012 | $error++; |
||
| 2013 | $errorforline++; |
||
| 2014 | $errorforinvoice[$key] = 'other'; |
||
| 2015 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 2016 | } |
||
| 2017 | } |
||
| 2018 | } |
||
| 2019 | } |
||
| 2020 | } |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | // Protection against a bug on lines before |
||
| 2024 | if (!$errorforline && (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT'))) { |
||
| 2025 | $error++; |
||
| 2026 | $errorforline++; |
||
| 2027 | $errorforinvoice[$key] = 'amountsnotbalanced'; |
||
| 2028 | setEventMessages('We tried to insert a non balanced transaction in book for ' . $val["ref"] . '. Canceled. Surely a bug.', null, 'errors'); |
||
| 2029 | } |
||
| 2030 | |||
| 2031 | if (!$errorforline) { |
||
| 2032 | $db->commit(); |
||
| 2033 | } else { |
||
| 2034 | $db->rollback(); |
||
| 2035 | |||
| 2036 | if ($error >= 10) { |
||
| 2037 | setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors'); |
||
| 2038 | break; // Break in the foreach |
||
| 2039 | } |
||
| 2040 | } |
||
| 2041 | } |
||
| 2042 | |||
| 2043 | $tabpay = $taber; |
||
| 2044 | |||
| 2045 | if (empty($error) && count($tabpay) > 0) { |
||
| 2046 | setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
||
| 2047 | } elseif (count($tabpay) == $error) { |
||
| 2048 | setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
||
| 2049 | } else { |
||
| 2050 | setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
||
| 2051 | } |
||
| 2052 | |||
| 2053 | $action = ''; |
||
| 2054 | |||
| 2055 | // Must reload data, so we make a redirect |
||
| 2056 | if (count($tabpay) != $error) { |
||
| 2057 | $param = 'id_journal=' . $id_journal; |
||
| 2058 | $param .= '&date_startday=' . $date_startday; |
||
| 2059 | $param .= '&date_startmonth=' . $date_startmonth; |
||
| 2060 | $param .= '&date_startyear=' . $date_startyear; |
||
| 2061 | $param .= '&date_endday=' . $date_endday; |
||
| 2062 | $param .= '&date_endmonth=' . $date_endmonth; |
||
| 2063 | $param .= '&date_endyear=' . $date_endyear; |
||
| 2064 | $param .= '&in_bookkeeping=' . $in_bookkeeping; |
||
| 2065 | |||
| 2066 | header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : '')); |
||
| 2067 | exit; |
||
| 2068 | } |
||
| 2069 | } |
||
| 2070 | |||
| 2071 | |||
| 2072 | /* |
||
| 2073 | * View |
||
| 2074 | */ |
||
| 2075 | |||
| 2076 | $form = new Form($db); |
||
| 2077 | |||
| 2078 | $userstatic = new User($db); |
||
| 2079 | |||
| 2080 | // Export |
||
| 2081 | if ($action == 'exportcsv' && !$error) { // ISO and not UTF8 ! |
||
| 2082 | $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
||
| 2083 | |||
| 2084 | $filename = 'journal'; |
||
| 2085 | $type_export = 'journal'; |
||
| 2086 | include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php'; |
||
| 2087 | |||
| 2088 | // CSV header line |
||
| 2089 | print '"' . $langs->transnoentitiesnoconv("Date") . '"' . $sep; |
||
| 2090 | print '"' . $langs->transnoentitiesnoconv("Piece") . '"' . $sep; |
||
| 2091 | print '"' . $langs->transnoentitiesnoconv("AccountAccounting") . '"' . $sep; |
||
| 2092 | print '"' . $langs->transnoentitiesnoconv("LabelOperation") . '"' . $sep; |
||
| 2093 | print '"' . $langs->transnoentitiesnoconv("AccountingDebit") . '"' . $sep; |
||
| 2094 | print '"' . $langs->transnoentitiesnoconv("AccountingCredit") . '"' . $sep; |
||
| 2095 | print "\n"; |
||
| 2096 | |||
| 2097 | foreach ($taber as $key => $val) { |
||
| 2098 | $date = dol_print_date($val["date"], 'day'); |
||
| 2099 | |||
| 2100 | $userstatic->id = $tabuser[$key]['id']; |
||
| 2101 | $userstatic->name = $tabuser[$key]['name']; |
||
| 2102 | |||
| 2103 | // Fees |
||
| 2104 | foreach ($tabht[$key] as $k => $mt) { |
||
| 2105 | $accountingaccount = new AccountingAccount($db); |
||
| 2106 | $accountingaccount->fetch(null, $k, true); |
||
| 2107 | if ($mt) { |
||
| 2108 | print '"' . $date . '"' . $sep; |
||
| 2109 | print '"' . $val["ref"] . '"' . $sep; |
||
| 2110 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 2111 | print '"' . dol_trunc($accountingaccount->label, 32) . '"' . $sep; |
||
| 2112 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 2113 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"'; |
||
| 2114 | print "\n"; |
||
| 2115 | } |
||
| 2116 | } |
||
| 2117 | |||
| 2118 | // VAT |
||
| 2119 | foreach ($tabtva[$key] as $k => $mt) { |
||
| 2120 | if ($mt) { |
||
| 2121 | print '"' . $date . '"' . $sep; |
||
| 2122 | print '"' . $val["ref"] . '"' . $sep; |
||
| 2123 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 2124 | print '"' . dol_trunc($langs->trans("VAT")) . '"' . $sep; |
||
| 2125 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 2126 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"'; |
||
| 2127 | print "\n"; |
||
| 2128 | } |
||
| 2129 | } |
||
| 2130 | |||
| 2131 | // Third party |
||
| 2132 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 2133 | print '"' . $date . '"' . $sep; |
||
| 2134 | print '"' . $val["ref"] . '"' . $sep; |
||
| 2135 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 2136 | print '"' . dol_trunc($userstatic->name) . '"' . $sep; |
||
| 2137 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 2138 | print '"' . ($mt >= 0 ? price($mt) : '') . '"'; |
||
| 2139 | } |
||
| 2140 | print "\n"; |
||
| 2141 | } |
||
| 2142 | } |
||
| 2143 | |||
| 2144 | if (empty($action) || $action == 'view') { |
||
| 2145 | $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
||
| 2146 | |||
| 2147 | llxHeader('', dol_string_nohtmltag($title)); |
||
| 2148 | |||
| 2149 | $nom = $title; |
||
| 2150 | $nomlink = ''; |
||
| 2151 | $periodlink = ''; |
||
| 2152 | $exportlink = ''; |
||
| 2153 | $builddate = dol_now(); |
||
| 2154 | $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>'; |
||
| 2155 | |||
| 2156 | $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger")); |
||
| 2157 | $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0) . ' - ' . $form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
||
| 2158 | $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
||
| 2159 | |||
| 2160 | $varlink = 'id_journal=' . $id_journal; |
||
| 2161 | |||
| 2162 | journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink); |
||
| 2163 | |||
| 2164 | if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
||
| 2165 | // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
||
| 2166 | // Fiscal period test |
||
| 2167 | $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "accounting_fiscalyear WHERE entity = " . ((int) $conf->entity); |
||
| 2168 | $resql = $db->query($sql); |
||
| 2169 | if ($resql) { |
||
| 2170 | $obj = $db->fetch_object($resql); |
||
| 2171 | if ($obj->nb == 0) { |
||
| 2172 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("TheFiscalPeriodIsNotDefined"); |
||
| 2173 | $desc = ' : ' . $langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
||
| 2174 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("FiscalPeriod") . '</strong>', $desc); |
||
| 2175 | print $desc; |
||
| 2176 | print '</div>'; |
||
| 2177 | } |
||
| 2178 | } else { |
||
| 2179 | dol_print_error($db); |
||
| 2180 | } |
||
| 2181 | } |
||
| 2182 | |||
| 2183 | // Button to write into Ledger |
||
| 2184 | if (!getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1') { |
||
| 2185 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
||
| 2186 | $desc = ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
||
| 2187 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>', $desc); |
||
| 2188 | print $desc; |
||
| 2189 | print '</div>'; |
||
| 2190 | } |
||
| 2191 | print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
||
| 2192 | |||
| 2193 | if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
||
| 2194 | print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />'; |
||
| 2195 | } |
||
| 2196 | if (!getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1') { |
||
| 2197 | print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />'; |
||
| 2198 | } else { |
||
| 2199 | if ($in_bookkeeping == 'notyet') { |
||
| 2200 | print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />'; |
||
| 2201 | } else { |
||
| 2202 | print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>'; |
||
| 2203 | } |
||
| 2204 | } |
||
| 2205 | print '</div>'; |
||
| 2206 | |||
| 2207 | // TODO Avoid using js. We can use a direct link with $param |
||
| 2208 | print ' |
||
| 2209 | <script type="text/javascript"> |
||
| 2210 | function launch_export() { |
||
| 2211 | $("div.fiche form input[name=\"action\"]").val("exportcsv"); |
||
| 2212 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 2213 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 2214 | } |
||
| 2215 | function writebookkeeping() { |
||
| 2216 | console.log("click on writebookkeeping"); |
||
| 2217 | $("div.fiche form input[name=\"action\"]").val("writebookkeeping"); |
||
| 2218 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 2219 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 2220 | } |
||
| 2221 | </script>'; |
||
| 2222 | |||
| 2223 | /* |
||
| 2224 | * Show result array |
||
| 2225 | */ |
||
| 2226 | print '<br>'; |
||
| 2227 | |||
| 2228 | $i = 0; |
||
| 2229 | print '<div class="div-table-responsive">'; |
||
| 2230 | print "<table class=\"noborder\" width=\"100%\">"; |
||
| 2231 | print "<tr class=\"liste_titre\">"; |
||
| 2232 | print "<td>" . $langs->trans("Date") . "</td>"; |
||
| 2233 | print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("ExpenseReportRef") . ")</td>"; |
||
| 2234 | print "<td>" . $langs->trans("AccountAccounting") . "</td>"; |
||
| 2235 | print "<td>" . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 2236 | print "<td>" . $langs->trans("LabelOperation") . "</td>"; |
||
| 2237 | print '<td class="right">' . $langs->trans("AccountingDebit") . "</td>"; |
||
| 2238 | print '<td class="right">' . $langs->trans("AccountingCredit") . "</td>"; |
||
| 2239 | print "</tr>\n"; |
||
| 2240 | |||
| 2241 | $i = 0; |
||
| 2242 | |||
| 2243 | $expensereportstatic = new ExpenseReport($db); |
||
| 2244 | $expensereportlinestatic = new ExpenseReportLine($db); |
||
| 2245 | |||
| 2246 | foreach ($taber as $key => $val) { |
||
| 2247 | $expensereportstatic->id = $key; |
||
| 2248 | $expensereportstatic->ref = $val["ref"]; |
||
| 2249 | $expensereportlinestatic->comments = html_entity_decode(dol_trunc($val["comments"], 32)); |
||
| 2250 | |||
| 2251 | $date = dol_print_date($val["date"], 'day'); |
||
| 2252 | |||
| 2253 | if ($errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 2254 | print '<tr class="oddeven">'; |
||
| 2255 | print "<!-- Some lines are not bound -->"; |
||
| 2256 | print "<td>" . $date . "</td>"; |
||
| 2257 | print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>"; |
||
| 2258 | // Account |
||
| 2259 | print "<td>"; |
||
| 2260 | print '<span class="error">' . $langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']) . '</span>'; |
||
| 2261 | print '</td>'; |
||
| 2262 | // Subledger account |
||
| 2263 | print "<td>"; |
||
| 2264 | print '</td>'; |
||
| 2265 | print "<td>"; |
||
| 2266 | print "</td>"; |
||
| 2267 | print '<td class="right"></td>'; |
||
| 2268 | print '<td class="right"></td>'; |
||
| 2269 | print "</tr>"; |
||
| 2270 | |||
| 2271 | $i++; |
||
| 2272 | } |
||
| 2273 | |||
| 2274 | // Fees |
||
| 2275 | foreach ($tabht[$key] as $k => $mt) { |
||
| 2276 | $accountingaccount = new AccountingAccount($db); |
||
| 2277 | $accountingaccount->fetch(null, $k, true); |
||
| 2278 | |||
| 2279 | if ($mt) { |
||
| 2280 | print '<tr class="oddeven">'; |
||
| 2281 | print "<!-- Fees -->"; |
||
| 2282 | print "<td>" . $date . "</td>"; |
||
| 2283 | print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>"; |
||
| 2284 | $userstatic->id = $tabuser[$key]['id']; |
||
| 2285 | $userstatic->name = $tabuser[$key]['name']; |
||
| 2286 | // Account |
||
| 2287 | print "<td>"; |
||
| 2288 | $accountoshow = length_accountg($k); |
||
| 2289 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 2290 | print '<span class="error">' . $langs->trans("FeeAccountNotDefined") . '</span>'; |
||
| 2291 | } else { |
||
| 2292 | print $accountoshow; |
||
| 2293 | } |
||
| 2294 | print '</td>'; |
||
| 2295 | // Subledger account |
||
| 2296 | print "<td>"; |
||
| 2297 | print '</td>'; |
||
| 2298 | $userstatic->id = $tabuser[$key]['id']; |
||
| 2299 | $userstatic->name = $tabuser[$key]['name']; |
||
| 2300 | print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $accountingaccount->label . "</td>"; |
||
| 2301 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 2302 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 2303 | print "</tr>"; |
||
| 2304 | |||
| 2305 | $i++; |
||
| 2306 | } |
||
| 2307 | } |
||
| 2308 | |||
| 2309 | // Third party |
||
| 2310 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 2311 | $userstatic->id = $tabuser[$key]['id']; |
||
| 2312 | $userstatic->name = $tabuser[$key]['name']; |
||
| 2313 | |||
| 2314 | print '<tr class="oddeven">'; |
||
| 2315 | print "<!-- Thirdparty -->"; |
||
| 2316 | print "<td>" . $date . "</td>"; |
||
| 2317 | print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>"; |
||
| 2318 | // Account |
||
| 2319 | print "<td>"; |
||
| 2320 | $accountoshow = length_accountg(getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT')); |
||
| 2321 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 2322 | print '<span class="error">' . $langs->trans("MainAccountForUsersNotDefined") . '</span>'; |
||
| 2323 | } else { |
||
| 2324 | print $accountoshow; |
||
| 2325 | } |
||
| 2326 | print "</td>"; |
||
| 2327 | // Subledger account |
||
| 2328 | print "<td>"; |
||
| 2329 | $accountoshow = length_accounta($k); |
||
| 2330 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 2331 | print '<span class="error">' . $langs->trans("UserAccountNotDefined") . '</span>'; |
||
| 2332 | } else { |
||
| 2333 | print $accountoshow; |
||
| 2334 | } |
||
| 2335 | print '</td>'; |
||
| 2336 | print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 2337 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 2338 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 2339 | print "</tr>"; |
||
| 2340 | |||
| 2341 | $i++; |
||
| 2342 | } |
||
| 2343 | |||
| 2344 | // VAT |
||
| 2345 | $listoftax = array(0, 1, 2); |
||
| 2346 | foreach ($listoftax as $numtax) { |
||
| 2347 | $arrayofvat = $tabtva; |
||
| 2348 | if ($numtax == 1) { |
||
| 2349 | $arrayofvat = $tablocaltax1; |
||
| 2350 | } |
||
| 2351 | if ($numtax == 2) { |
||
| 2352 | $arrayofvat = $tablocaltax2; |
||
| 2353 | } |
||
| 2354 | |||
| 2355 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 2356 | if ($mt) { |
||
| 2357 | print '<tr class="oddeven">'; |
||
| 2358 | print "<!-- VAT -->"; |
||
| 2359 | print "<td>" . $date . "</td>"; |
||
| 2360 | print "<td>" . $expensereportstatic->getNomUrl(1) . "</td>"; |
||
| 2361 | // Account |
||
| 2362 | print "<td>"; |
||
| 2363 | $accountoshow = length_accountg($k); |
||
| 2364 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 2365 | print '<span class="error">' . $langs->trans("VATAccountNotDefined") . '</span>'; |
||
| 2366 | } else { |
||
| 2367 | print $accountoshow; |
||
| 2368 | } |
||
| 2369 | print "</td>"; |
||
| 2370 | // Subledger account |
||
| 2371 | print "<td>"; |
||
| 2372 | print '</td>'; |
||
| 2373 | print "<td>" . $userstatic->getNomUrl(0, 'user', 16) . ' - ' . $langs->trans("VAT") . ' ' . implode(', ', $def_tva[$key][$k]) . ' %' . ($numtax ? ' - Localtax ' . $numtax : ''); |
||
| 2374 | print "</td>"; |
||
| 2375 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 2376 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 2377 | print "</tr>"; |
||
| 2378 | |||
| 2379 | $i++; |
||
| 2380 | } |
||
| 2381 | } |
||
| 2382 | } |
||
| 2383 | } |
||
| 2384 | |||
| 2385 | if (!$i) { |
||
| 2386 | $colspan = 7; |
||
| 2387 | print '<tr class="oddeven"><td colspan="' . $colspan . '"><span class="opacitymedium">' . $langs->trans("NoRecordFound") . '</span></td></tr>'; |
||
| 2388 | } |
||
| 2389 | |||
| 2390 | print "</table>"; |
||
| 2391 | print '</div>'; |
||
| 2392 | |||
| 2393 | // End of page |
||
| 2394 | llxFooter(); |
||
| 2395 | } |
||
| 2396 | $db->close(); |
||
| 2397 | } |
||
| 2398 | |||
| 2399 | /** |
||
| 2400 | * \file htdocs/accountancy/journal/purchasesjournal.php |
||
| 2401 | * \ingroup Accountancy (Double entries) |
||
| 2402 | * \brief Page with purchases journal |
||
| 2403 | */ |
||
| 2404 | public function purchasesjournal() |
||
| 2405 | { |
||
| 2406 | global $conf; |
||
| 2407 | global $db; |
||
| 2408 | global $user; |
||
| 2409 | global $hookmanager; |
||
| 2410 | global $user; |
||
| 2411 | global $menumanager; |
||
| 2412 | global $langs; |
||
| 2413 | global $mysoc; |
||
| 2414 | |||
| 2415 | // Load translation files required by the page |
||
| 2416 | $langs->loadLangs(array("commercial", "compta", "bills", "other", "accountancy", "errors")); |
||
| 2417 | |||
| 2418 | $id_journal = GETPOSTINT('id_journal'); |
||
| 2419 | $action = GETPOST('action', 'aZ09'); |
||
| 2420 | |||
| 2421 | $date_startmonth = GETPOST('date_startmonth'); |
||
| 2422 | $date_startday = GETPOST('date_startday'); |
||
| 2423 | $date_startyear = GETPOST('date_startyear'); |
||
| 2424 | $date_endmonth = GETPOST('date_endmonth'); |
||
| 2425 | $date_endday = GETPOST('date_endday'); |
||
| 2426 | $date_endyear = GETPOST('date_endyear'); |
||
| 2427 | $in_bookkeeping = GETPOST('in_bookkeeping'); |
||
| 2428 | if ($in_bookkeeping == '') { |
||
| 2429 | $in_bookkeeping = 'notyet'; |
||
| 2430 | } |
||
| 2431 | |||
| 2432 | $now = dol_now(); |
||
| 2433 | |||
| 2434 | $hookmanager->initHooks(array('purchasesjournal')); |
||
| 2435 | $parameters = array(); |
||
| 2436 | |||
| 2437 | // Security check |
||
| 2438 | if (!isModEnabled('accounting')) { |
||
| 2439 | accessforbidden(); |
||
| 2440 | } |
||
| 2441 | if ($user->socid > 0) { |
||
| 2442 | accessforbidden(); |
||
| 2443 | } |
||
| 2444 | if (!$user->hasRight('accounting', 'mouvements', 'lire')) { |
||
| 2445 | accessforbidden(); |
||
| 2446 | } |
||
| 2447 | |||
| 2448 | $error = 0; |
||
| 2449 | |||
| 2450 | |||
| 2451 | /* |
||
| 2452 | * Actions |
||
| 2453 | */ |
||
| 2454 | |||
| 2455 | $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks |
||
| 2456 | |||
| 2457 | $accountingaccount = new AccountingAccount($db); |
||
| 2458 | |||
| 2459 | // Get information of journal |
||
| 2460 | $accountingjournalstatic = new AccountingJournal($db); |
||
| 2461 | $accountingjournalstatic->fetch($id_journal); |
||
| 2462 | $journal = $accountingjournalstatic->code; |
||
| 2463 | $journal_label = $accountingjournalstatic->label; |
||
| 2464 | |||
| 2465 | $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); |
||
| 2466 | $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); |
||
| 2467 | |||
| 2468 | if (empty($date_startmonth)) { |
||
| 2469 | // Period by default on transfer |
||
| 2470 | $dates = getDefaultDatesForTransfer(); |
||
| 2471 | $date_start = $dates['date_start']; |
||
| 2472 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 2473 | $pastmonth = $dates['pastmonth']; |
||
| 2474 | } |
||
| 2475 | if (empty($date_endmonth)) { |
||
| 2476 | // Period by default on transfer |
||
| 2477 | $dates = getDefaultDatesForTransfer(); |
||
| 2478 | $date_end = $dates['date_end']; |
||
| 2479 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 2480 | $pastmonth = $dates['pastmonth']; |
||
| 2481 | } |
||
| 2482 | |||
| 2483 | if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form |
||
| 2484 | $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); |
||
| 2485 | $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); |
||
| 2486 | } |
||
| 2487 | |||
| 2488 | $sql = "SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle as label, f.ref_supplier, f.date_lim_reglement as dlr, f.close_code, f.vat_reverse_charge,"; |
||
| 2489 | $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.tva as total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.vat_src_code, fd.info_bits,"; |
||
| 2490 | $sql .= " p.default_vat_code AS product_buy_default_vat_code, p.tva_tx as product_buy_vat, p.localtax1_tx as product_buy_localvat1, p.localtax2_tx as product_buy_localvat2,"; |
||
| 2491 | $sql .= " co.code as country_code, co.label as country_label,"; |
||
| 2492 | $sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.fk_pays,"; |
||
| 2493 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 2494 | $sql .= " spe.accountancy_code_customer as code_compta,"; |
||
| 2495 | $sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,"; |
||
| 2496 | } else { |
||
| 2497 | $sql .= " s.code_compta as code_compta,"; |
||
| 2498 | $sql .= " s.code_compta_fournisseur,"; |
||
| 2499 | } |
||
| 2500 | if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) { |
||
| 2501 | $sql .= " ppe.accountancy_code_buy,"; |
||
| 2502 | } else { |
||
| 2503 | $sql .= " p.accountancy_code_buy,"; |
||
| 2504 | } |
||
| 2505 | $sql .= " aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte"; |
||
| 2506 | $parameters = array(); |
||
| 2507 | $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook |
||
| 2508 | $sql .= $hookmanager->resPrint; |
||
| 2509 | $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd"; |
||
| 2510 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product"; |
||
| 2511 | if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) { |
||
| 2512 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity); |
||
| 2513 | } |
||
| 2514 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation"; |
||
| 2515 | $sql .= " JOIN " . MAIN_DB_PREFIX . "facture_fourn as f ON f.rowid = fd.fk_facture_fourn"; |
||
| 2516 | $sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc"; |
||
| 2517 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as co ON co.rowid = s.fk_pays "; |
||
| 2518 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 2519 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity); |
||
| 2520 | } |
||
| 2521 | $parameters = array(); |
||
| 2522 | $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters); // Note that $action and $object may have been modified by hook |
||
| 2523 | $sql .= $hookmanager->resPrint; |
||
| 2524 | $sql .= " WHERE f.fk_statut > 0"; |
||
| 2525 | $sql .= " AND fd.fk_code_ventilation > 0"; |
||
| 2526 | $sql .= " AND f.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy |
||
| 2527 | if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) { |
||
| 2528 | $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_SITUATION . ")"; |
||
| 2529 | } else { |
||
| 2530 | $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_DEPOSIT . "," . FactureFournisseur::TYPE_SITUATION . ")"; |
||
| 2531 | } |
||
| 2532 | if ($date_start && $date_end) { |
||
| 2533 | $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'"; |
||
| 2534 | } |
||
| 2535 | // Define begin binding date |
||
| 2536 | if (getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) { |
||
| 2537 | $sql .= " AND f.datef >= '" . $db->idate(getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) . "'"; |
||
| 2538 | } |
||
| 2539 | // Already in bookkeeping or not |
||
| 2540 | if ($in_bookkeeping == 'already') { |
||
| 2541 | $sql .= " AND f.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')"; |
||
| 2542 | } |
||
| 2543 | if ($in_bookkeeping == 'notyet') { |
||
| 2544 | $sql .= " AND f.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')"; |
||
| 2545 | } |
||
| 2546 | $parameters = array(); |
||
| 2547 | $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook |
||
| 2548 | $sql .= $hookmanager->resPrint; |
||
| 2549 | $sql .= " ORDER BY f.datef"; |
||
| 2550 | |||
| 2551 | dol_syslog('accountancy/journal/purchasesjournal.php', LOG_DEBUG); |
||
| 2552 | $result = $db->query($sql); |
||
| 2553 | if ($result) { |
||
| 2554 | $tabfac = array(); |
||
| 2555 | $tabht = array(); |
||
| 2556 | $tabtva = array(); |
||
| 2557 | $def_tva = array(); |
||
| 2558 | $tabttc = array(); |
||
| 2559 | $tablocaltax1 = array(); |
||
| 2560 | $tablocaltax2 = array(); |
||
| 2561 | $tabcompany = array(); |
||
| 2562 | $tabother = array(); |
||
| 2563 | $tabrctva = array(); |
||
| 2564 | $tabrclocaltax1 = array(); |
||
| 2565 | $tabrclocaltax2 = array(); |
||
| 2566 | $vatdata_cache = array(); |
||
| 2567 | |||
| 2568 | $num = $db->num_rows($result); |
||
| 2569 | |||
| 2570 | // Variables |
||
| 2571 | $cptfour = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER', 'NotDefined'); |
||
| 2572 | $cpttva = getDolGlobalString('ACCOUNTING_VAT_BUY_ACCOUNT', 'NotDefined'); |
||
| 2573 | $rcctva = getDolGlobalString('ACCOUNTING_VAT_BUY_REVERSE_CHARGES_CREDIT', 'NotDefined'); |
||
| 2574 | $rcdtva = getDolGlobalString('ACCOUNTING_VAT_BUY_REVERSE_CHARGES_DEBIT', 'NotDefined'); |
||
| 2575 | $country_code_in_EEC = getCountriesInEEC(); // This make a database call but there is a cache done into $conf->cache['country_code_in_EEC'] |
||
| 2576 | |||
| 2577 | $i = 0; |
||
| 2578 | while ($i < $num) { |
||
| 2579 | $obj = $db->fetch_object($result); |
||
| 2580 | |||
| 2581 | // Controls |
||
| 2582 | $compta_soc = ($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour; |
||
| 2583 | |||
| 2584 | $compta_prod = $obj->compte; |
||
| 2585 | if (empty($compta_prod)) { |
||
| 2586 | if ($obj->product_type == 0) { |
||
| 2587 | $compta_prod = getDolGlobalString('ACCOUNTING_PRODUCT_BUY_ACCOUNT', 'NotDefined'); |
||
| 2588 | } else { |
||
| 2589 | $compta_prod = getDolGlobalString('ACCOUNTING_SERVICE_BUY_ACCOUNT', 'NotDefined'); |
||
| 2590 | } |
||
| 2591 | } |
||
| 2592 | |||
| 2593 | $tax_id = $obj->tva_tx . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : ''); |
||
| 2594 | if (array_key_exists($tax_id, $vatdata_cache)) { |
||
| 2595 | $vatdata = $vatdata_cache[$tax_id]; |
||
| 2596 | } else { |
||
| 2597 | $vatdata = getTaxesFromId($tax_id, $mysoc, $mysoc, 0); |
||
| 2598 | $vatdata_cache[$tax_id] = $vatdata; |
||
| 2599 | } |
||
| 2600 | $compta_tva = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); |
||
| 2601 | $compta_localtax1 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); |
||
| 2602 | $compta_localtax2 = (!empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva); |
||
| 2603 | $compta_counterpart_tva_npr = getDolGlobalString('ACCOUNTING_COUNTERPART_VAT_NPR', 'NotDefined'); |
||
| 2604 | |||
| 2605 | // Define array to display all VAT rates that use this accounting account $compta_tva |
||
| 2606 | if (price2num($obj->tva_tx) || !empty($obj->vat_src_code)) { |
||
| 2607 | $def_tva[$obj->rowid][$compta_tva][vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')] = (vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')); |
||
| 2608 | } |
||
| 2609 | |||
| 2610 | //$line = new SupplierInvoiceLine($db); |
||
| 2611 | //$line->fetch($obj->fdid); |
||
| 2612 | |||
| 2613 | $tabfac[$obj->rowid]["date"] = $db->jdate($obj->df); |
||
| 2614 | $tabfac[$obj->rowid]["datereg"] = $db->jdate($obj->dlr); |
||
| 2615 | $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier . ' (' . $obj->ref . ')'; |
||
| 2616 | $tabfac[$obj->rowid]["refsologest"] = $obj->ref; |
||
| 2617 | $tabfac[$obj->rowid]["refsuppliersologest"] = $obj->ref_supplier; |
||
| 2618 | $tabfac[$obj->rowid]["type"] = $obj->type; |
||
| 2619 | $tabfac[$obj->rowid]["description"] = $obj->description; |
||
| 2620 | $tabfac[$obj->rowid]["close_code"] = $obj->close_code; // close_code = 'replaced' for replacement invoices (not used in most european countries) |
||
| 2621 | //$tabfac[$obj->rowid]["fk_facturefourndet"] = $obj->fdid; |
||
| 2622 | |||
| 2623 | // Avoid warnings |
||
| 2624 | if (!isset($tabttc[$obj->rowid][$compta_soc])) { |
||
| 2625 | $tabttc[$obj->rowid][$compta_soc] = 0; |
||
| 2626 | } |
||
| 2627 | if (!isset($tabht[$obj->rowid][$compta_prod])) { |
||
| 2628 | $tabht[$obj->rowid][$compta_prod] = 0; |
||
| 2629 | } |
||
| 2630 | if (!isset($tabtva[$obj->rowid][$compta_tva])) { |
||
| 2631 | $tabtva[$obj->rowid][$compta_tva] = 0; |
||
| 2632 | } |
||
| 2633 | if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) { |
||
| 2634 | $tablocaltax1[$obj->rowid][$compta_localtax1] = 0; |
||
| 2635 | } |
||
| 2636 | if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) { |
||
| 2637 | $tablocaltax2[$obj->rowid][$compta_localtax2] = 0; |
||
| 2638 | } |
||
| 2639 | |||
| 2640 | // VAT Reverse charge |
||
| 2641 | if (($mysoc->country_code == 'FR' || getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) && $obj->vat_reverse_charge == 1 && in_array($obj->country_code, $country_code_in_EEC)) { |
||
| 2642 | $rcvatdata = getTaxesFromId($obj->product_buy_vat . ($obj->product_buy_default_vat_code ? ' (' . $obj->product_buy_default_vat_code . ')' : ''), $mysoc, $mysoc, 0); |
||
| 2643 | $rcc_compta_tva = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva); |
||
| 2644 | $rcd_compta_tva = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva); |
||
| 2645 | $rcc_compta_localtax1 = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva); |
||
| 2646 | $rcd_compta_localtax1 = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva); |
||
| 2647 | $rcc_compta_localtax2 = (!empty($vatdata['accountancy_code_vat_reverse_charge_credit']) ? $vatdata['accountancy_code_vat_reverse_charge_credit'] : $rcctva); |
||
| 2648 | $rcd_compta_localtax2 = (!empty($vatdata['accountancy_code_vat_reverse_charge_debit']) ? $vatdata['accountancy_code_vat_reverse_charge_debit'] : $rcdtva); |
||
| 2649 | if (price2num($obj->product_buy_vat) || !empty($obj->product_buy_default_vat_code)) { |
||
| 2650 | $vat_key = vatrate($obj->product_buy_vat) . ($obj->product_buy_default_vat_code ? ' (' . $obj->product_buy_default_vat_code . ')' : ''); |
||
| 2651 | $val_value = $vat_key; |
||
| 2652 | $def_tva[$obj->rowid][$rcc_compta_tva][$vat_key] = $val_value; |
||
| 2653 | $def_tva[$obj->rowid][$rcd_compta_tva][$vat_key] = $val_value; |
||
| 2654 | } |
||
| 2655 | |||
| 2656 | if (!isset($tabrctva[$obj->rowid][$rcc_compta_tva])) { |
||
| 2657 | $tabrctva[$obj->rowid][$rcc_compta_tva] = 0; |
||
| 2658 | } |
||
| 2659 | if (!isset($tabrctva[$obj->rowid][$rcd_compta_tva])) { |
||
| 2660 | $tabrctva[$obj->rowid][$rcd_compta_tva] = 0; |
||
| 2661 | } |
||
| 2662 | if (!isset($tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1])) { |
||
| 2663 | $tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] = 0; |
||
| 2664 | } |
||
| 2665 | if (!isset($tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1])) { |
||
| 2666 | $tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] = 0; |
||
| 2667 | } |
||
| 2668 | if (!isset($tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2])) { |
||
| 2669 | $tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] = 0; |
||
| 2670 | } |
||
| 2671 | if (!isset($tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2])) { |
||
| 2672 | $tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] = 0; |
||
| 2673 | } |
||
| 2674 | |||
| 2675 | $rcvat = (float) price2num($obj->total_ttc * $obj->product_buy_vat / 100, 'MT'); |
||
| 2676 | $rclocalvat1 = (float) price2num($obj->total_ttc * $obj->product_buy_localvat1 / 100, 'MT'); |
||
| 2677 | $rclocalvat2 = (float) price2num($obj->total_ttc * $obj->product_buy_localvat2 / 100, 'MT'); |
||
| 2678 | |||
| 2679 | $tabrctva[$obj->rowid][$rcd_compta_tva] += $rcvat; |
||
| 2680 | $tabrctva[$obj->rowid][$rcc_compta_tva] -= $rcvat; |
||
| 2681 | $tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] += $rclocalvat1; |
||
| 2682 | $tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] -= $rclocalvat1; |
||
| 2683 | $tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] += $rclocalvat2; |
||
| 2684 | $tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] -= $rclocalvat2; |
||
| 2685 | } |
||
| 2686 | |||
| 2687 | $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc; |
||
| 2688 | $tabht[$obj->rowid][$compta_prod] += $obj->total_ht; |
||
| 2689 | $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva; |
||
| 2690 | $tva_npr = ((($obj->info_bits & 1) == 1) ? 1 : 0); |
||
| 2691 | if ($tva_npr) { // If NPR, we add an entry for counterpartWe into tabother |
||
| 2692 | $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva; |
||
| 2693 | } |
||
| 2694 | $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1; |
||
| 2695 | $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2; |
||
| 2696 | $tabcompany[$obj->rowid] = array( |
||
| 2697 | 'id' => $obj->socid, |
||
| 2698 | 'name' => $obj->name, |
||
| 2699 | 'code_fournisseur' => $obj->code_fournisseur, |
||
| 2700 | 'code_compta_fournisseur' => $compta_soc |
||
| 2701 | ); |
||
| 2702 | |||
| 2703 | $i++; |
||
| 2704 | } |
||
| 2705 | } else { |
||
| 2706 | dol_print_error($db); |
||
| 2707 | } |
||
| 2708 | |||
| 2709 | // Check for too many invoices first. |
||
| 2710 | if (count($tabfac) > 10000) { // Global config in htdocs/admin/const.php??? |
||
| 2711 | $error++; |
||
| 2712 | setEventMessages("TooManyInvoicesToProcessPleaseUseAMoreSelectiveFilter", null, 'errors'); |
||
| 2713 | } |
||
| 2714 | |||
| 2715 | $errorforinvoice = array(); |
||
| 2716 | |||
| 2717 | /* |
||
| 2718 | // Old way, 1 query for each invoice |
||
| 2719 | // Loop in invoices to detect lines with not binding lines |
||
| 2720 | foreach ($tabfac as $key => $val) { // Loop on each invoice |
||
| 2721 | $sql = "SELECT COUNT(fd.rowid) as nb"; |
||
| 2722 | $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as fd"; |
||
| 2723 | $sql .= " WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0"; |
||
| 2724 | $sql .= " AND fd.total_ttc <> 0 AND fk_facture_fourn = ".((int) $key); |
||
| 2725 | $resql = $db->query($sql); |
||
| 2726 | if ($resql) { |
||
| 2727 | $obj = $db->fetch_object($resql); |
||
| 2728 | if ($obj->nb > 0) { |
||
| 2729 | $errorforinvoice[$key] = 'somelinesarenotbound'; |
||
| 2730 | } |
||
| 2731 | } else { |
||
| 2732 | dol_print_error($db); |
||
| 2733 | } |
||
| 2734 | } |
||
| 2735 | */ |
||
| 2736 | // New way, single query, load all unbound lines |
||
| 2737 | $sql = " |
||
| 2738 | SELECT |
||
| 2739 | fk_facture_fourn, |
||
| 2740 | COUNT(fd.rowid) as nb |
||
| 2741 | FROM |
||
| 2742 | llx_facture_fourn_det as fd |
||
| 2743 | WHERE |
||
| 2744 | fd.product_type <= 2 |
||
| 2745 | AND fd.fk_code_ventilation <= 0 |
||
| 2746 | AND fd.total_ttc <> 0 |
||
| 2747 | AND fk_facture_fourn IN (" . $db->sanitize(implode(",", array_keys($tabfac))) . ") |
||
| 2748 | GROUP BY fk_facture_fourn |
||
| 2749 | "; |
||
| 2750 | $resql = $db->query($sql); |
||
| 2751 | |||
| 2752 | $num = $db->num_rows($resql); |
||
| 2753 | $i = 0; |
||
| 2754 | while ($i < $num) { |
||
| 2755 | $obj = $db->fetch_object($resql); |
||
| 2756 | if ($obj->nb > 0) { |
||
| 2757 | $errorforinvoice[$obj->fk_facture_fourn] = 'somelinesarenotbound'; |
||
| 2758 | } |
||
| 2759 | $i++; |
||
| 2760 | } |
||
| 2761 | //var_dump($errorforinvoice);exit; |
||
| 2762 | |||
| 2763 | |||
| 2764 | |||
| 2765 | // Bookkeeping Write |
||
| 2766 | if ($action == 'writebookkeeping' && !$error) { |
||
| 2767 | $now = dol_now(); |
||
| 2768 | $error = 0; |
||
| 2769 | |||
| 2770 | $companystatic = new Societe($db); |
||
| 2771 | $invoicestatic = new FactureFournisseur($db); |
||
| 2772 | $accountingaccountsupplier = new AccountingAccount($db); |
||
| 2773 | |||
| 2774 | $accountingaccountsupplier->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), true); |
||
| 2775 | |||
| 2776 | foreach ($tabfac as $key => $val) { // Loop on each invoice |
||
| 2777 | $errorforline = 0; |
||
| 2778 | |||
| 2779 | $totalcredit = 0; |
||
| 2780 | $totaldebit = 0; |
||
| 2781 | |||
| 2782 | $db->begin(); |
||
| 2783 | |||
| 2784 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 2785 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 2786 | $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur']; |
||
| 2787 | $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur']; |
||
| 2788 | $companystatic->fournisseur = 1; |
||
| 2789 | |||
| 2790 | $invoicestatic->id = $key; |
||
| 2791 | $invoicestatic->ref = (string) $val["refsologest"]; |
||
| 2792 | $invoicestatic->ref_supplier = $val["refsuppliersologest"]; |
||
| 2793 | $invoicestatic->type = $val["type"]; |
||
| 2794 | $invoicestatic->description = html_entity_decode(dol_trunc($val["description"], 32)); |
||
| 2795 | $invoicestatic->close_code = $val["close_code"]; |
||
| 2796 | |||
| 2797 | $date = dol_print_date($val["date"], 'day'); |
||
| 2798 | |||
| 2799 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 2800 | $replacedinvoice = 0; |
||
| 2801 | if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) { |
||
| 2802 | $replacedinvoice = 1; |
||
| 2803 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 2804 | if ($alreadydispatched) { |
||
| 2805 | $replacedinvoice = 2; |
||
| 2806 | } |
||
| 2807 | } |
||
| 2808 | |||
| 2809 | // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted) |
||
| 2810 | if ($replacedinvoice == 1) { |
||
| 2811 | $db->rollback(); |
||
| 2812 | continue; |
||
| 2813 | } |
||
| 2814 | |||
| 2815 | // Error if some lines are not binded/ready to be journalized |
||
| 2816 | if ($errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 2817 | $error++; |
||
| 2818 | $errorforline++; |
||
| 2819 | setEventMessages($langs->trans('ErrorInvoiceContainsLinesNotYetBounded', $val['ref']), null, 'errors'); |
||
| 2820 | } |
||
| 2821 | |||
| 2822 | // Thirdparty |
||
| 2823 | if (!$errorforline) { |
||
| 2824 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 2825 | $bookkeeping = new BookKeeping($db); |
||
| 2826 | $bookkeeping->doc_date = $val["date"]; |
||
| 2827 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 2828 | $bookkeeping->doc_ref = $val["refsologest"]; |
||
| 2829 | $bookkeeping->date_creation = $now; |
||
| 2830 | $bookkeeping->doc_type = 'supplier_invoice'; |
||
| 2831 | $bookkeeping->fk_doc = $key; |
||
| 2832 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 2833 | $bookkeeping->thirdparty_code = $companystatic->code_fournisseur; |
||
| 2834 | |||
| 2835 | $bookkeeping->subledger_account = $tabcompany[$key]['code_compta_fournisseur']; |
||
| 2836 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 2837 | |||
| 2838 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
||
| 2839 | $bookkeeping->label_compte = $accountingaccountsupplier->label; |
||
| 2840 | |||
| 2841 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount"); |
||
| 2842 | $bookkeeping->montant = $mt; |
||
| 2843 | $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D'; |
||
| 2844 | $bookkeeping->debit = ($mt <= 0) ? -$mt : 0; |
||
| 2845 | $bookkeeping->credit = ($mt > 0) ? $mt : 0; |
||
| 2846 | $bookkeeping->code_journal = $journal; |
||
| 2847 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 2848 | $bookkeeping->fk_user_author = $user->id; |
||
| 2849 | $bookkeeping->entity = $conf->entity; |
||
| 2850 | |||
| 2851 | $totaldebit += $bookkeeping->debit; |
||
| 2852 | $totalcredit += $bookkeeping->credit; |
||
| 2853 | |||
| 2854 | $result = $bookkeeping->create($user); |
||
| 2855 | if ($result < 0) { |
||
| 2856 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 2857 | $error++; |
||
| 2858 | $errorforline++; |
||
| 2859 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 2860 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 2861 | } else { |
||
| 2862 | $error++; |
||
| 2863 | $errorforline++; |
||
| 2864 | $errorforinvoice[$key] = 'other'; |
||
| 2865 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 2866 | } |
||
| 2867 | } else { |
||
| 2868 | if (getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && getDolGlobalInt('ACCOUNTING_ENABLE_AUTOLETTERING')) { |
||
| 2869 | require_once DOL_DOCUMENT_ROOT . '/accountancy/class/lettering.class.php'; |
||
| 2870 | $lettering_static = new Lettering($db); |
||
| 2871 | |||
| 2872 | $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id)); |
||
| 2873 | } |
||
| 2874 | } |
||
| 2875 | } |
||
| 2876 | } |
||
| 2877 | |||
| 2878 | // Product / Service |
||
| 2879 | if (!$errorforline) { |
||
| 2880 | foreach ($tabht[$key] as $k => $mt) { |
||
| 2881 | $resultfetch = $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 2882 | $label_account = $accountingaccount->label; |
||
| 2883 | |||
| 2884 | // get compte id and label |
||
| 2885 | if ($resultfetch > 0) { |
||
| 2886 | $bookkeeping = new BookKeeping($db); |
||
| 2887 | $bookkeeping->doc_date = $val["date"]; |
||
| 2888 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 2889 | $bookkeeping->doc_ref = $val["refsologest"]; |
||
| 2890 | $bookkeeping->date_creation = $now; |
||
| 2891 | $bookkeeping->doc_type = 'supplier_invoice'; |
||
| 2892 | $bookkeeping->fk_doc = $key; |
||
| 2893 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 2894 | $bookkeeping->thirdparty_code = $companystatic->code_fournisseur; |
||
| 2895 | |||
| 2896 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT')) { |
||
| 2897 | if ($k == getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT')) { |
||
| 2898 | $bookkeeping->subledger_account = $tabcompany[$key]['code_compta']; |
||
| 2899 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 2900 | } else { |
||
| 2901 | $bookkeeping->subledger_account = ''; |
||
| 2902 | $bookkeeping->subledger_label = ''; |
||
| 2903 | } |
||
| 2904 | } else { |
||
| 2905 | $bookkeeping->subledger_account = ''; |
||
| 2906 | $bookkeeping->subledger_label = ''; |
||
| 2907 | } |
||
| 2908 | |||
| 2909 | $bookkeeping->numero_compte = $k; |
||
| 2910 | $bookkeeping->label_compte = $label_account; |
||
| 2911 | |||
| 2912 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $label_account; |
||
| 2913 | $bookkeeping->montant = $mt; |
||
| 2914 | $bookkeeping->sens = ($mt < 0) ? 'C' : 'D'; |
||
| 2915 | $bookkeeping->debit = ($mt > 0) ? $mt : 0; |
||
| 2916 | $bookkeeping->credit = ($mt <= 0) ? -$mt : 0; |
||
| 2917 | $bookkeeping->code_journal = $journal; |
||
| 2918 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 2919 | $bookkeeping->fk_user_author = $user->id; |
||
| 2920 | $bookkeeping->entity = $conf->entity; |
||
| 2921 | |||
| 2922 | $totaldebit += $bookkeeping->debit; |
||
| 2923 | $totalcredit += $bookkeeping->credit; |
||
| 2924 | |||
| 2925 | $result = $bookkeeping->create($user); |
||
| 2926 | if ($result < 0) { |
||
| 2927 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 2928 | $error++; |
||
| 2929 | $errorforline++; |
||
| 2930 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 2931 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 2932 | } else { |
||
| 2933 | $error++; |
||
| 2934 | $errorforline++; |
||
| 2935 | $errorforinvoice[$key] = 'other'; |
||
| 2936 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 2937 | } |
||
| 2938 | } |
||
| 2939 | } |
||
| 2940 | } |
||
| 2941 | } |
||
| 2942 | |||
| 2943 | // VAT |
||
| 2944 | // var_dump($tabtva); |
||
| 2945 | if (!$errorforline) { |
||
| 2946 | $listoftax = array(0, 1, 2); |
||
| 2947 | foreach ($listoftax as $numtax) { |
||
| 2948 | $arrayofvat = $tabtva; |
||
| 2949 | if ($numtax == 1) { |
||
| 2950 | $arrayofvat = $tablocaltax1; |
||
| 2951 | } |
||
| 2952 | if ($numtax == 2) { |
||
| 2953 | $arrayofvat = $tablocaltax2; |
||
| 2954 | } |
||
| 2955 | |||
| 2956 | // VAT Reverse charge |
||
| 2957 | if ($mysoc->country_code == 'FR' || getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) { |
||
| 2958 | $has_vat = false; |
||
| 2959 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 2960 | if ($mt) { |
||
| 2961 | $has_vat = true; |
||
| 2962 | } |
||
| 2963 | } |
||
| 2964 | |||
| 2965 | if (!$has_vat) { |
||
| 2966 | $arrayofvat = $tabrctva; |
||
| 2967 | if ($numtax == 1) { |
||
| 2968 | $arrayofvat = $tabrclocaltax1; |
||
| 2969 | } |
||
| 2970 | if ($numtax == 2) { |
||
| 2971 | $arrayofvat = $tabrclocaltax2; |
||
| 2972 | } |
||
| 2973 | if (!is_array($arrayofvat[$key])) { |
||
| 2974 | $arrayofvat[$key] = array(); |
||
| 2975 | } |
||
| 2976 | } |
||
| 2977 | } |
||
| 2978 | |||
| 2979 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 2980 | if ($mt) { |
||
| 2981 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache for label |
||
| 2982 | $label_account = $accountingaccount->label; |
||
| 2983 | |||
| 2984 | $bookkeeping = new BookKeeping($db); |
||
| 2985 | $bookkeeping->doc_date = $val["date"]; |
||
| 2986 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 2987 | $bookkeeping->doc_ref = $val["refsologest"]; |
||
| 2988 | $bookkeeping->date_creation = $now; |
||
| 2989 | $bookkeeping->doc_type = 'supplier_invoice'; |
||
| 2990 | $bookkeeping->fk_doc = $key; |
||
| 2991 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 2992 | $bookkeeping->thirdparty_code = $companystatic->code_fournisseur; |
||
| 2993 | |||
| 2994 | $bookkeeping->subledger_account = ''; |
||
| 2995 | $bookkeeping->subledger_label = ''; |
||
| 2996 | |||
| 2997 | $bookkeeping->numero_compte = $k; |
||
| 2998 | $bookkeeping->label_compte = $label_account; |
||
| 2999 | |||
| 3000 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . ' ' . implode(', ', $def_tva[$key][$k]) . ' %' . ($numtax ? ' - Localtax ' . $numtax : ''); |
||
| 3001 | $bookkeeping->montant = $mt; |
||
| 3002 | $bookkeeping->sens = ($mt < 0) ? 'C' : 'D'; |
||
| 3003 | $bookkeeping->debit = ($mt > 0) ? $mt : 0; |
||
| 3004 | $bookkeeping->credit = ($mt <= 0) ? -$mt : 0; |
||
| 3005 | $bookkeeping->code_journal = $journal; |
||
| 3006 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 3007 | $bookkeeping->fk_user_author = $user->id; |
||
| 3008 | $bookkeeping->entity = $conf->entity; |
||
| 3009 | |||
| 3010 | $totaldebit += $bookkeeping->debit; |
||
| 3011 | $totalcredit += $bookkeeping->credit; |
||
| 3012 | |||
| 3013 | $result = $bookkeeping->create($user); |
||
| 3014 | if ($result < 0) { |
||
| 3015 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 3016 | $error++; |
||
| 3017 | $errorforline++; |
||
| 3018 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 3019 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 3020 | } else { |
||
| 3021 | $error++; |
||
| 3022 | $errorforline++; |
||
| 3023 | $errorforinvoice[$key] = 'other'; |
||
| 3024 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 3025 | } |
||
| 3026 | } |
||
| 3027 | } |
||
| 3028 | } |
||
| 3029 | } |
||
| 3030 | } |
||
| 3031 | |||
| 3032 | // Counterpart of VAT for VAT NPR |
||
| 3033 | // var_dump($tabother); |
||
| 3034 | if (!$errorforline && is_array($tabother[$key])) { |
||
| 3035 | foreach ($tabother[$key] as $k => $mt) { |
||
| 3036 | if ($mt) { |
||
| 3037 | $bookkeeping = new BookKeeping($db); |
||
| 3038 | $bookkeeping->doc_date = $val["date"]; |
||
| 3039 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 3040 | $bookkeeping->doc_ref = $val["refsologest"]; |
||
| 3041 | $bookkeeping->date_creation = $now; |
||
| 3042 | $bookkeeping->doc_type = 'supplier_invoice'; |
||
| 3043 | $bookkeeping->fk_doc = $key; |
||
| 3044 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 3045 | $bookkeeping->thirdparty_code = $companystatic->code_fournisseur; |
||
| 3046 | |||
| 3047 | $bookkeeping->subledger_account = ''; |
||
| 3048 | $bookkeeping->subledger_label = ''; |
||
| 3049 | |||
| 3050 | $bookkeeping->numero_compte = $k; |
||
| 3051 | |||
| 3052 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . ' NPR'; |
||
| 3053 | $bookkeeping->montant = $mt; |
||
| 3054 | $bookkeeping->sens = ($mt < 0) ? 'C' : 'D'; |
||
| 3055 | $bookkeeping->debit = ($mt > 0) ? $mt : 0; |
||
| 3056 | $bookkeeping->credit = ($mt <= 0) ? -$mt : 0; |
||
| 3057 | $bookkeeping->code_journal = $journal; |
||
| 3058 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 3059 | $bookkeeping->fk_user_author = $user->id; |
||
| 3060 | $bookkeeping->entity = $conf->entity; |
||
| 3061 | |||
| 3062 | $totaldebit += $bookkeeping->debit; |
||
| 3063 | $totalcredit += $bookkeeping->credit; |
||
| 3064 | |||
| 3065 | $result = $bookkeeping->create($user); |
||
| 3066 | if ($result < 0) { |
||
| 3067 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 3068 | $error++; |
||
| 3069 | $errorforline++; |
||
| 3070 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 3071 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 3072 | } else { |
||
| 3073 | $error++; |
||
| 3074 | $errorforline++; |
||
| 3075 | $errorforinvoice[$key] = 'other'; |
||
| 3076 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 3077 | } |
||
| 3078 | } |
||
| 3079 | } |
||
| 3080 | } |
||
| 3081 | } |
||
| 3082 | |||
| 3083 | // Protection against a bug on lines before |
||
| 3084 | if (!$errorforline && (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT'))) { |
||
| 3085 | $error++; |
||
| 3086 | $errorforline++; |
||
| 3087 | $errorforinvoice[$key] = 'amountsnotbalanced'; |
||
| 3088 | setEventMessages('We tried to insert a non balanced transaction in book for ' . $invoicestatic->ref . '. Canceled. Surely a bug.', null, 'errors'); |
||
| 3089 | } |
||
| 3090 | |||
| 3091 | if (!$errorforline) { |
||
| 3092 | $db->commit(); |
||
| 3093 | } else { |
||
| 3094 | $db->rollback(); |
||
| 3095 | |||
| 3096 | if ($error >= 10) { |
||
| 3097 | setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors'); |
||
| 3098 | break; // Break in the foreach |
||
| 3099 | } |
||
| 3100 | } |
||
| 3101 | } |
||
| 3102 | |||
| 3103 | $tabpay = $tabfac; |
||
| 3104 | |||
| 3105 | if (empty($error) && count($tabpay) > 0) { |
||
| 3106 | setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
||
| 3107 | } elseif (count($tabpay) == $error) { |
||
| 3108 | setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
||
| 3109 | } else { |
||
| 3110 | setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
||
| 3111 | } |
||
| 3112 | |||
| 3113 | $action = ''; |
||
| 3114 | |||
| 3115 | // Must reload data, so we make a redirect |
||
| 3116 | if (count($tabpay) != $error) { |
||
| 3117 | $param = 'id_journal=' . $id_journal; |
||
| 3118 | $param .= '&date_startday=' . $date_startday; |
||
| 3119 | $param .= '&date_startmonth=' . $date_startmonth; |
||
| 3120 | $param .= '&date_startyear=' . $date_startyear; |
||
| 3121 | $param .= '&date_endday=' . $date_endday; |
||
| 3122 | $param .= '&date_endmonth=' . $date_endmonth; |
||
| 3123 | $param .= '&date_endyear=' . $date_endyear; |
||
| 3124 | $param .= '&in_bookkeeping=' . $in_bookkeeping; |
||
| 3125 | header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : '')); |
||
| 3126 | exit; |
||
| 3127 | } |
||
| 3128 | } |
||
| 3129 | |||
| 3130 | /* |
||
| 3131 | * View |
||
| 3132 | */ |
||
| 3133 | |||
| 3134 | $form = new Form($db); |
||
| 3135 | |||
| 3136 | // Export |
||
| 3137 | if ($action == 'exportcsv' && !$error) { // ISO and not UTF8 ! |
||
| 3138 | $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
||
| 3139 | |||
| 3140 | $filename = 'journal'; |
||
| 3141 | $type_export = 'journal'; |
||
| 3142 | include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php'; |
||
| 3143 | |||
| 3144 | $companystatic = new Fournisseur($db); |
||
| 3145 | $invoicestatic = new FactureFournisseur($db); |
||
| 3146 | |||
| 3147 | foreach ($tabfac as $key => $val) { |
||
| 3148 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 3149 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 3150 | $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur']; |
||
| 3151 | $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur']; |
||
| 3152 | $companystatic->fournisseur = 1; |
||
| 3153 | |||
| 3154 | $invoicestatic->id = $key; |
||
| 3155 | $invoicestatic->ref = $val["refsologest"]; |
||
| 3156 | $invoicestatic->ref_supplier = $val["refsuppliersologest"]; |
||
| 3157 | $invoicestatic->type = $val["type"]; |
||
| 3158 | $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32); |
||
| 3159 | $invoicestatic->close_code = $val["close_code"]; |
||
| 3160 | |||
| 3161 | $date = dol_print_date($val["date"], 'day'); |
||
| 3162 | |||
| 3163 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 3164 | $replacedinvoice = 0; |
||
| 3165 | if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) { |
||
| 3166 | $replacedinvoice = 1; |
||
| 3167 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 3168 | if ($alreadydispatched) { |
||
| 3169 | $replacedinvoice = 2; |
||
| 3170 | } |
||
| 3171 | } |
||
| 3172 | |||
| 3173 | // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted) |
||
| 3174 | if ($replacedinvoice == 1) { |
||
| 3175 | continue; |
||
| 3176 | } |
||
| 3177 | |||
| 3178 | // Third party |
||
| 3179 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 3180 | //if ($mt) { |
||
| 3181 | print '"' . $key . '"' . $sep; |
||
| 3182 | print '"' . $date . '"' . $sep; |
||
| 3183 | print '"' . $val["refsologest"] . '"' . $sep; |
||
| 3184 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 3185 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 3186 | print '"' . length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER')) . '"' . $sep; |
||
| 3187 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 3188 | print '"' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 3189 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 3190 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 3191 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 3192 | print '"' . $journal . '"'; |
||
| 3193 | print "\n"; |
||
| 3194 | //} |
||
| 3195 | } |
||
| 3196 | |||
| 3197 | // Product / Service |
||
| 3198 | foreach ($tabht[$key] as $k => $mt) { |
||
| 3199 | $accountingaccount = new AccountingAccount($db); |
||
| 3200 | $accountingaccount->fetch(null, $k, true); |
||
| 3201 | //if ($mt) { |
||
| 3202 | print '"' . $key . '"' . $sep; |
||
| 3203 | print '"' . $date . '"' . $sep; |
||
| 3204 | print '"' . $val["refsologest"] . '"' . $sep; |
||
| 3205 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 3206 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3207 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3208 | print '""' . $sep; |
||
| 3209 | print '"' . mb_convert_encoding(dol_trunc($accountingaccount->label, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 3210 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $val["refsuppliersologest"] . ' - ' . dol_trunc($accountingaccount->label, 32) . '"' . $sep; |
||
| 3211 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 3212 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 3213 | print '"' . $journal . '"'; |
||
| 3214 | print "\n"; |
||
| 3215 | //} |
||
| 3216 | } |
||
| 3217 | |||
| 3218 | // VAT |
||
| 3219 | $listoftax = array(0, 1, 2); |
||
| 3220 | foreach ($listoftax as $numtax) { |
||
| 3221 | $arrayofvat = $tabtva; |
||
| 3222 | if ($numtax == 1) { |
||
| 3223 | $arrayofvat = $tablocaltax1; |
||
| 3224 | } |
||
| 3225 | if ($numtax == 2) { |
||
| 3226 | $arrayofvat = $tablocaltax2; |
||
| 3227 | } |
||
| 3228 | |||
| 3229 | // VAT Reverse charge |
||
| 3230 | if ($mysoc->country_code == 'FR' || getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) { |
||
| 3231 | $has_vat = false; |
||
| 3232 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 3233 | if ($mt) { |
||
| 3234 | $has_vat = true; |
||
| 3235 | } |
||
| 3236 | } |
||
| 3237 | |||
| 3238 | if (!$has_vat) { |
||
| 3239 | $arrayofvat = $tabrctva; |
||
| 3240 | if ($numtax == 1) { |
||
| 3241 | $arrayofvat = $tabrclocaltax1; |
||
| 3242 | } |
||
| 3243 | if ($numtax == 2) { |
||
| 3244 | $arrayofvat = $tabrclocaltax2; |
||
| 3245 | } |
||
| 3246 | if (!is_array($arrayofvat[$key])) { |
||
| 3247 | $arrayofvat[$key] = array(); |
||
| 3248 | } |
||
| 3249 | } |
||
| 3250 | } |
||
| 3251 | |||
| 3252 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 3253 | if ($mt) { |
||
| 3254 | print '"' . $key . '"' . $sep; |
||
| 3255 | print '"' . $date . '"' . $sep; |
||
| 3256 | print '"' . $val["refsologest"] . '"' . $sep; |
||
| 3257 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 3258 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3259 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3260 | print '""' . $sep; |
||
| 3261 | print '"' . $langs->trans("VAT") . ' - ' . implode(', ', $def_tva[$key][$k]) . ' %"' . $sep; |
||
| 3262 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . implode(', ', $def_tva[$key][$k]) . ' %' . ($numtax ? ' - Localtax ' . $numtax : '') . '"' . $sep; |
||
| 3263 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 3264 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 3265 | print '"' . $journal . '"'; |
||
| 3266 | print "\n"; |
||
| 3267 | } |
||
| 3268 | } |
||
| 3269 | |||
| 3270 | // VAT counterpart for NPR |
||
| 3271 | if (is_array($tabother[$key])) { |
||
| 3272 | foreach ($tabother[$key] as $k => $mt) { |
||
| 3273 | if ($mt) { |
||
| 3274 | print '"' . $key . '"' . $sep; |
||
| 3275 | print '"' . $date . '"' . $sep; |
||
| 3276 | print '"' . $val["refsologest"] . '"' . $sep; |
||
| 3277 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 3278 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3279 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3280 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 3281 | print '"' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 3282 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . ' NPR"' . $sep; |
||
| 3283 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 3284 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 3285 | print '"' . $journal . '"'; |
||
| 3286 | print "\n"; |
||
| 3287 | } |
||
| 3288 | } |
||
| 3289 | } |
||
| 3290 | } |
||
| 3291 | } |
||
| 3292 | } |
||
| 3293 | |||
| 3294 | if (empty($action) || $action == 'view') { |
||
| 3295 | $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
||
| 3296 | |||
| 3297 | llxHeader('', dol_string_nohtmltag($title)); |
||
| 3298 | |||
| 3299 | $nom = $title; |
||
| 3300 | $nomlink = ''; |
||
| 3301 | $periodlink = ''; |
||
| 3302 | $exportlink = ''; |
||
| 3303 | $builddate = dol_now(); |
||
| 3304 | $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>'; |
||
| 3305 | if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) { |
||
| 3306 | $description .= $langs->trans("DepositsAreNotIncluded"); |
||
| 3307 | } else { |
||
| 3308 | $description .= $langs->trans("DepositsAreIncluded"); |
||
| 3309 | } |
||
| 3310 | |||
| 3311 | $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger")); |
||
| 3312 | $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0) . ' - ' . $form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
||
| 3313 | $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
||
| 3314 | |||
| 3315 | $varlink = 'id_journal=' . $id_journal; |
||
| 3316 | |||
| 3317 | journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink); |
||
| 3318 | |||
| 3319 | if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
||
| 3320 | // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
||
| 3321 | // Fiscal period test |
||
| 3322 | $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "accounting_fiscalyear WHERE entity = " . ((int) $conf->entity); |
||
| 3323 | $resql = $db->query($sql); |
||
| 3324 | if ($resql) { |
||
| 3325 | $obj = $db->fetch_object($resql); |
||
| 3326 | if ($obj->nb == 0) { |
||
| 3327 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("TheFiscalPeriodIsNotDefined"); |
||
| 3328 | $desc = ' : ' . $langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
||
| 3329 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("FiscalPeriod") . '</strong>', $desc); |
||
| 3330 | print $desc; |
||
| 3331 | print '</div>'; |
||
| 3332 | } |
||
| 3333 | } else { |
||
| 3334 | dol_print_error($db); |
||
| 3335 | } |
||
| 3336 | } |
||
| 3337 | |||
| 3338 | // Button to write into Ledger |
||
| 3339 | $acctSupplierNotConfigured = in_array(getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), ['','-1']); |
||
| 3340 | if ($acctSupplierNotConfigured) { |
||
| 3341 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
||
| 3342 | $desc = ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
||
| 3343 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>', $desc); |
||
| 3344 | print $desc; |
||
| 3345 | print '</div>'; |
||
| 3346 | } |
||
| 3347 | print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
||
| 3348 | if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
||
| 3349 | print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />'; |
||
| 3350 | } |
||
| 3351 | if ($acctSupplierNotConfigured) { |
||
| 3352 | print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />'; |
||
| 3353 | } else { |
||
| 3354 | if ($in_bookkeeping == 'notyet') { |
||
| 3355 | print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />'; |
||
| 3356 | } else { |
||
| 3357 | print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>'; |
||
| 3358 | } |
||
| 3359 | } |
||
| 3360 | print '</div>'; |
||
| 3361 | |||
| 3362 | // TODO Avoid using js. We can use a direct link with $param |
||
| 3363 | print ' |
||
| 3364 | <script type="text/javascript"> |
||
| 3365 | function launch_export() { |
||
| 3366 | $("div.fiche form input[name=\"action\"]").val("exportcsv"); |
||
| 3367 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 3368 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 3369 | } |
||
| 3370 | function writebookkeeping() { |
||
| 3371 | console.log("click on writebookkeeping"); |
||
| 3372 | $("div.fiche form input[name=\"action\"]").val("writebookkeeping"); |
||
| 3373 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 3374 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 3375 | } |
||
| 3376 | </script>'; |
||
| 3377 | |||
| 3378 | /* |
||
| 3379 | * Show result array |
||
| 3380 | */ |
||
| 3381 | print '<br>'; |
||
| 3382 | |||
| 3383 | print '<div class="div-table-responsive">'; |
||
| 3384 | print "<table class=\"noborder\" width=\"100%\">"; |
||
| 3385 | print "<tr class=\"liste_titre\">"; |
||
| 3386 | print "<td>" . $langs->trans("Date") . "</td>"; |
||
| 3387 | print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>"; |
||
| 3388 | print "<td>" . $langs->trans("AccountAccounting") . "</td>"; |
||
| 3389 | print "<td>" . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 3390 | print "<td>" . $langs->trans("LabelOperation") . "</td>"; |
||
| 3391 | print '<td class="center">' . $langs->trans("AccountingDebit") . "</td>"; |
||
| 3392 | print '<td class="center">' . $langs->trans("AccountingCredit") . "</td>"; |
||
| 3393 | print "</tr>\n"; |
||
| 3394 | |||
| 3395 | $i = 0; |
||
| 3396 | |||
| 3397 | $invoicestatic = new FactureFournisseur($db); |
||
| 3398 | $companystatic = new Fournisseur($db); |
||
| 3399 | |||
| 3400 | foreach ($tabfac as $key => $val) { |
||
| 3401 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 3402 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 3403 | $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur']; |
||
| 3404 | $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur']; |
||
| 3405 | $companystatic->fournisseur = 1; |
||
| 3406 | |||
| 3407 | $invoicestatic->id = $key; |
||
| 3408 | $invoicestatic->ref = $val["refsologest"]; |
||
| 3409 | $invoicestatic->ref_supplier = $val["refsuppliersologest"]; |
||
| 3410 | $invoicestatic->type = $val["type"]; |
||
| 3411 | $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32); |
||
| 3412 | $invoicestatic->close_code = $val["close_code"]; |
||
| 3413 | |||
| 3414 | $date = dol_print_date($val["date"], 'day'); |
||
| 3415 | |||
| 3416 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 3417 | $replacedinvoice = 0; |
||
| 3418 | if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) { |
||
| 3419 | $replacedinvoice = 1; |
||
| 3420 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 3421 | if ($alreadydispatched) { |
||
| 3422 | $replacedinvoice = 2; |
||
| 3423 | } |
||
| 3424 | } |
||
| 3425 | |||
| 3426 | // If not already into bookkeeping, we won't add it, if yes, add the counterpart ???. |
||
| 3427 | if ($replacedinvoice == 1) { |
||
| 3428 | print '<tr class="oddeven">'; |
||
| 3429 | print "<!-- Replaced invoice -->"; |
||
| 3430 | print "<td>" . $date . "</td>"; |
||
| 3431 | print "<td><strike>" . $invoicestatic->getNomUrl(1) . "</strike></td>"; |
||
| 3432 | // Account |
||
| 3433 | print "<td>"; |
||
| 3434 | print $langs->trans("Replaced"); |
||
| 3435 | print '</td>'; |
||
| 3436 | // Subledger account |
||
| 3437 | print "<td>"; |
||
| 3438 | print '</td>'; |
||
| 3439 | print "<td>"; |
||
| 3440 | print "</td>"; |
||
| 3441 | print '<td class="right"></td>'; |
||
| 3442 | print '<td class="right"></td>'; |
||
| 3443 | print "</tr>"; |
||
| 3444 | |||
| 3445 | $i++; |
||
| 3446 | continue; |
||
| 3447 | } |
||
| 3448 | if ($errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 3449 | print '<tr class="oddeven">'; |
||
| 3450 | print "<!-- Some lines are not bound -->"; |
||
| 3451 | print "<td>" . $date . "</td>"; |
||
| 3452 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 3453 | // Account |
||
| 3454 | print "<td>"; |
||
| 3455 | print '<span class="error">' . $langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']) . '</span>'; |
||
| 3456 | print '</td>'; |
||
| 3457 | // Subledger account |
||
| 3458 | print "<td>"; |
||
| 3459 | print '</td>'; |
||
| 3460 | print "<td>"; |
||
| 3461 | print "</td>"; |
||
| 3462 | print '<td class="right"></td>'; |
||
| 3463 | print '<td class="right"></td>'; |
||
| 3464 | print "</tr>"; |
||
| 3465 | |||
| 3466 | $i++; |
||
| 3467 | } |
||
| 3468 | |||
| 3469 | // Third party |
||
| 3470 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 3471 | print '<tr class="oddeven">'; |
||
| 3472 | print "<!-- Thirdparty -->"; |
||
| 3473 | print "<td>" . $date . "</td>"; |
||
| 3474 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 3475 | // Account |
||
| 3476 | print "<td>"; |
||
| 3477 | $accountoshow = length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER')); |
||
| 3478 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 3479 | print '<span class="error">' . $langs->trans("MainAccountForSuppliersNotDefined") . '</span>'; |
||
| 3480 | } else { |
||
| 3481 | print $accountoshow; |
||
| 3482 | } |
||
| 3483 | print '</td>'; |
||
| 3484 | // Subledger account |
||
| 3485 | print "<td>"; |
||
| 3486 | $accountoshow = length_accounta($k); |
||
| 3487 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 3488 | print '<span class="error">' . $langs->trans("ThirdpartyAccountNotDefined") . '</span>'; |
||
| 3489 | } else { |
||
| 3490 | print $accountoshow; |
||
| 3491 | } |
||
| 3492 | print '</td>'; |
||
| 3493 | print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 3494 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 3495 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 3496 | print "</tr>"; |
||
| 3497 | |||
| 3498 | $i++; |
||
| 3499 | } |
||
| 3500 | |||
| 3501 | // Product / Service |
||
| 3502 | foreach ($tabht[$key] as $k => $mt) { |
||
| 3503 | $accountingaccount = new AccountingAccount($db); |
||
| 3504 | $accountingaccount->fetch(null, $k, true); |
||
| 3505 | |||
| 3506 | print '<tr class="oddeven">'; |
||
| 3507 | print "<!-- Product -->"; |
||
| 3508 | print "<td>" . $date . "</td>"; |
||
| 3509 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 3510 | // Account |
||
| 3511 | print "<td>"; |
||
| 3512 | $accountoshow = length_accountg($k); |
||
| 3513 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 3514 | print '<span class="error">' . $langs->trans("ProductAccountNotDefined") . '</span>'; |
||
| 3515 | } else { |
||
| 3516 | print $accountoshow; |
||
| 3517 | } |
||
| 3518 | print "</td>"; |
||
| 3519 | // Subledger account |
||
| 3520 | print "<td>"; |
||
| 3521 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT')) { |
||
| 3522 | if ($k == getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER_DEPOSIT')) { |
||
| 3523 | print length_accounta($tabcompany[$key]['code_compta']); |
||
| 3524 | } |
||
| 3525 | } elseif (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 3526 | print '<span class="error">' . $langs->trans("ThirdpartyAccountNotDefined") . '</span>'; |
||
| 3527 | } |
||
| 3528 | print '</td>'; |
||
| 3529 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 3530 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 3531 | print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $accountingaccount->label . "</td>"; |
||
| 3532 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 3533 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 3534 | print "</tr>"; |
||
| 3535 | |||
| 3536 | $i++; |
||
| 3537 | } |
||
| 3538 | |||
| 3539 | // VAT |
||
| 3540 | $listoftax = array(0, 1, 2); |
||
| 3541 | foreach ($listoftax as $numtax) { |
||
| 3542 | $arrayofvat = $tabtva; |
||
| 3543 | if ($numtax == 1) { |
||
| 3544 | $arrayofvat = $tablocaltax1; |
||
| 3545 | } |
||
| 3546 | if ($numtax == 2) { |
||
| 3547 | $arrayofvat = $tablocaltax2; |
||
| 3548 | } |
||
| 3549 | |||
| 3550 | // VAT Reverse charge |
||
| 3551 | if ($mysoc->country_code == 'FR' || getDolGlobalString('ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) { |
||
| 3552 | $has_vat = false; |
||
| 3553 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 3554 | if ($mt) { |
||
| 3555 | $has_vat = true; |
||
| 3556 | } |
||
| 3557 | } |
||
| 3558 | |||
| 3559 | if (!$has_vat) { |
||
| 3560 | $arrayofvat = $tabrctva; |
||
| 3561 | if ($numtax == 1) { |
||
| 3562 | $arrayofvat = $tabrclocaltax1; |
||
| 3563 | } |
||
| 3564 | if ($numtax == 2) { |
||
| 3565 | $arrayofvat = $tabrclocaltax2; |
||
| 3566 | } |
||
| 3567 | if (!is_array($arrayofvat[$key])) { |
||
| 3568 | $arrayofvat[$key] = array(); |
||
| 3569 | } |
||
| 3570 | } |
||
| 3571 | } |
||
| 3572 | |||
| 3573 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 3574 | if ($mt) { |
||
| 3575 | print '<tr class="oddeven">'; |
||
| 3576 | print "<!-- VAT -->"; |
||
| 3577 | print "<td>" . $date . "</td>"; |
||
| 3578 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 3579 | // Account |
||
| 3580 | print "<td>"; |
||
| 3581 | $accountoshow = length_accountg($k); |
||
| 3582 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 3583 | print '<span class="error">' . $langs->trans("VATAccountNotDefined") . ' (' . $langs->trans("AccountingJournalType3") . ')</span>'; |
||
| 3584 | } else { |
||
| 3585 | print $accountoshow; |
||
| 3586 | } |
||
| 3587 | print "</td>"; |
||
| 3588 | // Subledger account |
||
| 3589 | print "<td>"; |
||
| 3590 | print '</td>'; |
||
| 3591 | print "<td>"; |
||
| 3592 | print $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . ' ' . implode(', ', $def_tva[$key][$k]) . ' %' . ($numtax ? ' - Localtax ' . $numtax : ''); |
||
| 3593 | print "</td>"; |
||
| 3594 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 3595 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 3596 | print "</tr>"; |
||
| 3597 | |||
| 3598 | $i++; |
||
| 3599 | } |
||
| 3600 | } |
||
| 3601 | } |
||
| 3602 | |||
| 3603 | // VAT counterpart for NPR |
||
| 3604 | if (is_array($tabother[$key])) { |
||
| 3605 | foreach ($tabother[$key] as $k => $mt) { |
||
| 3606 | if ($mt) { |
||
| 3607 | print '<tr class="oddeven">'; |
||
| 3608 | print '<!-- VAT counterpart NPR -->'; |
||
| 3609 | print "<td>" . $date . "</td>"; |
||
| 3610 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 3611 | // Account |
||
| 3612 | print '<td>'; |
||
| 3613 | $accountoshow = length_accountg($k); |
||
| 3614 | if ($accountoshow == '' || $accountoshow == 'NotDefined') { |
||
| 3615 | print '<span class="error">' . $langs->trans("VATAccountNotDefined") . ' (' . $langs->trans("NPR counterpart") . '). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account</span>'; |
||
| 3616 | } else { |
||
| 3617 | print $accountoshow; |
||
| 3618 | } |
||
| 3619 | print '</td>'; |
||
| 3620 | // Subledger account |
||
| 3621 | print "<td>"; |
||
| 3622 | print '</td>'; |
||
| 3623 | print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . " NPR (counterpart)</td>"; |
||
| 3624 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 3625 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 3626 | print "</tr>"; |
||
| 3627 | |||
| 3628 | $i++; |
||
| 3629 | } |
||
| 3630 | } |
||
| 3631 | } |
||
| 3632 | } |
||
| 3633 | |||
| 3634 | if (!$i) { |
||
| 3635 | print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">' . $langs->trans("NoRecordFound") . '</span></td></tr>'; |
||
| 3636 | } |
||
| 3637 | |||
| 3638 | print "</table>"; |
||
| 3639 | print '</div>'; |
||
| 3640 | |||
| 3641 | // End of page |
||
| 3642 | llxFooter(); |
||
| 3643 | } |
||
| 3644 | $db->close(); |
||
| 3645 | } |
||
| 3646 | |||
| 3647 | /** |
||
| 3648 | * \file htdocs/accountancy/journal/sellsjournal.php |
||
| 3649 | * \ingroup Accountancy (Double entries) |
||
| 3650 | * \brief Page with sells journal |
||
| 3651 | */ |
||
| 3652 | public function sellsjournal() |
||
| 3653 | { |
||
| 3654 | global $conf; |
||
| 3655 | global $db; |
||
| 3656 | global $user; |
||
| 3657 | global $hookmanager; |
||
| 3658 | global $user; |
||
| 3659 | global $menumanager; |
||
| 3660 | global $langs; |
||
| 3661 | global $mysoc; |
||
| 3662 | |||
| 3663 | // Load translation files required by the page |
||
| 3664 | $langs->loadLangs(array("commercial", "compta", "bills", "other", "accountancy", "errors")); |
||
| 3665 | |||
| 3666 | $id_journal = GETPOSTINT('id_journal'); |
||
| 3667 | $action = GETPOST('action', 'aZ09'); |
||
| 3668 | |||
| 3669 | $date_startmonth = GETPOST('date_startmonth'); |
||
| 3670 | $date_startday = GETPOST('date_startday'); |
||
| 3671 | $date_startyear = GETPOST('date_startyear'); |
||
| 3672 | $date_endmonth = GETPOST('date_endmonth'); |
||
| 3673 | $date_endday = GETPOST('date_endday'); |
||
| 3674 | $date_endyear = GETPOST('date_endyear'); |
||
| 3675 | $in_bookkeeping = GETPOST('in_bookkeeping'); |
||
| 3676 | if ($in_bookkeeping == '') { |
||
| 3677 | $in_bookkeeping = 'notyet'; |
||
| 3678 | } |
||
| 3679 | |||
| 3680 | $now = dol_now(); |
||
| 3681 | |||
| 3682 | $hookmanager->initHooks(array('sellsjournal')); |
||
| 3683 | $parameters = array(); |
||
| 3684 | |||
| 3685 | // Security check |
||
| 3686 | if (!isModEnabled('accounting')) { |
||
| 3687 | accessforbidden(); |
||
| 3688 | } |
||
| 3689 | if ($user->socid > 0) { |
||
| 3690 | accessforbidden(); |
||
| 3691 | } |
||
| 3692 | if (!$user->hasRight('accounting', 'mouvements', 'lire')) { |
||
| 3693 | accessforbidden(); |
||
| 3694 | } |
||
| 3695 | |||
| 3696 | $error = 0; |
||
| 3697 | |||
| 3698 | |||
| 3699 | /* |
||
| 3700 | * Actions |
||
| 3701 | */ |
||
| 3702 | |||
| 3703 | $reshook = $hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks |
||
| 3704 | |||
| 3705 | $accountingaccount = new AccountingAccount($db); |
||
| 3706 | |||
| 3707 | // Get information of journal |
||
| 3708 | $accountingjournalstatic = new AccountingJournal($db); |
||
| 3709 | $accountingjournalstatic->fetch($id_journal); |
||
| 3710 | $journal = $accountingjournalstatic->code; |
||
| 3711 | $journal_label = $accountingjournalstatic->label; |
||
| 3712 | |||
| 3713 | $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); |
||
| 3714 | $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); |
||
| 3715 | |||
| 3716 | if (empty($date_startmonth)) { |
||
| 3717 | // Period by default on transfer |
||
| 3718 | $dates = getDefaultDatesForTransfer(); |
||
| 3719 | $date_start = $dates['date_start']; |
||
| 3720 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 3721 | $pastmonth = $dates['pastmonth']; |
||
| 3722 | } |
||
| 3723 | if (empty($date_endmonth)) { |
||
| 3724 | // Period by default on transfer |
||
| 3725 | $dates = getDefaultDatesForTransfer(); |
||
| 3726 | $date_end = $dates['date_end']; |
||
| 3727 | $pastmonthyear = $dates['pastmonthyear']; |
||
| 3728 | $pastmonth = $dates['pastmonth']; |
||
| 3729 | } |
||
| 3730 | if (getDolGlobalString('ACCOUNTANCY_JOURNAL_USE_CURRENT_MONTH')) { |
||
| 3731 | $pastmonth += 1; |
||
| 3732 | } |
||
| 3733 | |||
| 3734 | if (!GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) { // We define date_start and date_end, only if we did not submit the form |
||
| 3735 | $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false); |
||
| 3736 | $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false); |
||
| 3737 | } |
||
| 3738 | |||
| 3739 | $sql = "SELECT f.rowid, f.ref, f.type, f.situation_cycle_ref, f.datef as df, f.ref_client, f.date_lim_reglement as dlr, f.close_code, f.retained_warranty, f.revenuestamp,"; |
||
| 3740 | $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.situation_percent, fd.vat_src_code, fd.info_bits,"; |
||
| 3741 | $sql .= " s.rowid as socid, s.nom as name, s.code_client, s.code_fournisseur,"; |
||
| 3742 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 3743 | $sql .= " spe.accountancy_code_customer as code_compta,"; |
||
| 3744 | $sql .= " spe.accountancy_code_supplier as code_compta_fournisseur,"; |
||
| 3745 | } else { |
||
| 3746 | $sql .= " s.code_compta as code_compta,"; |
||
| 3747 | $sql .= " s.code_compta_fournisseur,"; |
||
| 3748 | } |
||
| 3749 | $sql .= " p.rowid as pid, p.ref as pref, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte,"; |
||
| 3750 | if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) { |
||
| 3751 | $sql .= " ppe.accountancy_code_sell"; |
||
| 3752 | } else { |
||
| 3753 | $sql .= " p.accountancy_code_sell"; |
||
| 3754 | } |
||
| 3755 | $parameters = array(); |
||
| 3756 | $reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook |
||
| 3757 | $sql .= $hookmanager->resPrint; |
||
| 3758 | $sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd"; |
||
| 3759 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product"; |
||
| 3760 | if (getDolGlobalString('MAIN_PRODUCT_PERENTITY_SHARED')) { |
||
| 3761 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity); |
||
| 3762 | } |
||
| 3763 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation"; |
||
| 3764 | $sql .= " JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture"; |
||
| 3765 | $sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc"; |
||
| 3766 | if (getDolGlobalString('MAIN_COMPANY_PERENTITY_SHARED')) { |
||
| 3767 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity); |
||
| 3768 | } |
||
| 3769 | $parameters = array(); |
||
| 3770 | $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters); // Note that $action and $object may have been modified by hook |
||
| 3771 | $sql .= $hookmanager->resPrint; |
||
| 3772 | $sql .= " WHERE fd.fk_code_ventilation > 0"; |
||
| 3773 | $sql .= " AND f.entity IN (" . getEntity('invoice', 0) . ')'; // We don't share object for accountancy, we use source object sharing |
||
| 3774 | $sql .= " AND f.fk_statut > 0"; |
||
| 3775 | if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { // Non common setup |
||
| 3776 | $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_SITUATION . ")"; |
||
| 3777 | } else { |
||
| 3778 | $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_DEPOSIT . "," . Facture::TYPE_SITUATION . ")"; |
||
| 3779 | } |
||
| 3780 | $sql .= " AND fd.product_type IN (0,1)"; |
||
| 3781 | if ($date_start && $date_end) { |
||
| 3782 | $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'"; |
||
| 3783 | } |
||
| 3784 | // Define begin binding date |
||
| 3785 | if (getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) { |
||
| 3786 | $sql .= " AND f.datef >= '" . $db->idate(getDolGlobalString('ACCOUNTING_DATE_START_BINDING')) . "'"; |
||
| 3787 | } |
||
| 3788 | // Already in bookkeeping or not |
||
| 3789 | if ($in_bookkeeping == 'already') { |
||
| 3790 | $sql .= " AND f.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='customer_invoice')"; |
||
| 3791 | // $sql .= " AND fd.rowid IN (SELECT fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='customer_invoice')"; // Useless, we save one line for all products with same account |
||
| 3792 | } |
||
| 3793 | if ($in_bookkeeping == 'notyet') { |
||
| 3794 | $sql .= " AND f.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='customer_invoice')"; |
||
| 3795 | // $sql .= " AND fd.rowid NOT IN (SELECT fk_docdet FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='customer_invoice')"; // Useless, we save one line for all products with same account |
||
| 3796 | } |
||
| 3797 | $parameters = array(); |
||
| 3798 | $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook |
||
| 3799 | $sql .= $hookmanager->resPrint; |
||
| 3800 | $sql .= " ORDER BY f.datef, f.ref"; |
||
| 3801 | //print $sql; |
||
| 3802 | |||
| 3803 | dol_syslog('accountancy/journal/sellsjournal.php', LOG_DEBUG); |
||
| 3804 | $result = $db->query($sql); |
||
| 3805 | if ($result) { |
||
| 3806 | $tabfac = array(); |
||
| 3807 | $tabht = array(); |
||
| 3808 | $tabtva = array(); |
||
| 3809 | $def_tva = array(); |
||
| 3810 | $tabwarranty = array(); |
||
| 3811 | $tabrevenuestamp = array(); |
||
| 3812 | $tabttc = array(); |
||
| 3813 | $tablocaltax1 = array(); |
||
| 3814 | $tablocaltax2 = array(); |
||
| 3815 | $tabcompany = array(); |
||
| 3816 | $vatdata_cache = array(); |
||
| 3817 | |||
| 3818 | $num = $db->num_rows($result); |
||
| 3819 | |||
| 3820 | // Variables |
||
| 3821 | $cptcli = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER', 'NotDefined'); |
||
| 3822 | $cpttva = getDolGlobalString('ACCOUNTING_VAT_SOLD_ACCOUNT', 'NotDefined'); |
||
| 3823 | |||
| 3824 | $i = 0; |
||
| 3825 | while ($i < $num) { |
||
| 3826 | $obj = $db->fetch_object($result); |
||
| 3827 | |||
| 3828 | // Controls |
||
| 3829 | $compta_soc = (!empty($obj->code_compta)) ? $obj->code_compta : $cptcli; |
||
| 3830 | |||
| 3831 | $compta_prod = $obj->compte; |
||
| 3832 | if (empty($compta_prod)) { |
||
| 3833 | if ($obj->product_type == 0) { |
||
| 3834 | $compta_prod = getDolGlobalString('ACCOUNTING_PRODUCT_SOLD_ACCOUNT', 'NotDefined'); |
||
| 3835 | } else { |
||
| 3836 | $compta_prod = getDolGlobalString('ACCOUNTING_SERVICE_SOLD_ACCOUNT', 'NotDefined'); |
||
| 3837 | } |
||
| 3838 | } |
||
| 3839 | |||
| 3840 | //$compta_revenuestamp = getDolGlobalString('ACCOUNTING_REVENUESTAMP_SOLD_ACCOUNT', 'NotDefined'); |
||
| 3841 | |||
| 3842 | $tax_id = $obj->tva_tx . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : ''); |
||
| 3843 | if (array_key_exists($tax_id, $vatdata_cache)) { |
||
| 3844 | $vatdata = $vatdata_cache[$tax_id]; |
||
| 3845 | } else { |
||
| 3846 | $vatdata = getTaxesFromId($tax_id, $mysoc, $mysoc, 0); |
||
| 3847 | $vatdata_cache[$tax_id] = $vatdata; |
||
| 3848 | } |
||
| 3849 | $compta_tva = (!empty($vatdata['accountancy_code_sell']) ? $vatdata['accountancy_code_sell'] : $cpttva); |
||
| 3850 | $compta_localtax1 = (!empty($vatdata['accountancy_code_sell']) ? $vatdata['accountancy_code_sell'] : $cpttva); |
||
| 3851 | $compta_localtax2 = (!empty($vatdata['accountancy_code_sell']) ? $vatdata['accountancy_code_sell'] : $cpttva); |
||
| 3852 | |||
| 3853 | // Define the array to store the detail of each vat rate and code for lines |
||
| 3854 | if (price2num($obj->tva_tx) || !empty($obj->vat_src_code)) { |
||
| 3855 | $def_tva[$obj->rowid][$compta_tva][vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')] = (vatrate($obj->tva_tx) . ($obj->vat_src_code ? ' (' . $obj->vat_src_code . ')' : '')); |
||
| 3856 | } |
||
| 3857 | |||
| 3858 | // Create a compensation rate for situation invoice. |
||
| 3859 | $situation_ratio = 1; |
||
| 3860 | if (getDolGlobalInt('INVOICE_USE_SITUATION') == 1) { |
||
| 3861 | if ($obj->situation_cycle_ref) { |
||
| 3862 | // Avoid divide by 0 |
||
| 3863 | if ($obj->situation_percent == 0) { |
||
| 3864 | $situation_ratio = 0; |
||
| 3865 | } else { |
||
| 3866 | $line = new FactureLigne($db); |
||
| 3867 | $line->fetch($obj->fdid); |
||
| 3868 | |||
| 3869 | // Situation invoices handling |
||
| 3870 | $prev_progress = $line->get_prev_progress($obj->rowid); |
||
| 3871 | |||
| 3872 | $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent; |
||
| 3873 | } |
||
| 3874 | } |
||
| 3875 | } |
||
| 3876 | |||
| 3877 | $revenuestamp = (float) price2num($obj->revenuestamp, 'MT'); |
||
| 3878 | |||
| 3879 | // Invoice lines |
||
| 3880 | $tabfac[$obj->rowid]["date"] = $db->jdate($obj->df); |
||
| 3881 | $tabfac[$obj->rowid]["datereg"] = $db->jdate($obj->dlr); |
||
| 3882 | $tabfac[$obj->rowid]["ref"] = $obj->ref; |
||
| 3883 | $tabfac[$obj->rowid]["type"] = $obj->type; |
||
| 3884 | $tabfac[$obj->rowid]["description"] = $obj->label_compte; |
||
| 3885 | $tabfac[$obj->rowid]["close_code"] = $obj->close_code; // close_code = 'replaced' for replacement invoices (not used in most european countries) |
||
| 3886 | $tabfac[$obj->rowid]["revenuestamp"] = $revenuestamp; |
||
| 3887 | //$tabfac[$obj->rowid]["fk_facturedet"] = $obj->fdid; |
||
| 3888 | |||
| 3889 | // Avoid warnings |
||
| 3890 | if (!isset($tabttc[$obj->rowid][$compta_soc])) { |
||
| 3891 | $tabttc[$obj->rowid][$compta_soc] = 0; |
||
| 3892 | } |
||
| 3893 | if (!isset($tabht[$obj->rowid][$compta_prod])) { |
||
| 3894 | $tabht[$obj->rowid][$compta_prod] = 0; |
||
| 3895 | } |
||
| 3896 | if (!isset($tabtva[$obj->rowid][$compta_tva])) { |
||
| 3897 | $tabtva[$obj->rowid][$compta_tva] = 0; |
||
| 3898 | } |
||
| 3899 | if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) { |
||
| 3900 | $tablocaltax1[$obj->rowid][$compta_localtax1] = 0; |
||
| 3901 | } |
||
| 3902 | if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) { |
||
| 3903 | $tablocaltax2[$obj->rowid][$compta_localtax2] = 0; |
||
| 3904 | } |
||
| 3905 | |||
| 3906 | // Compensation of data for invoice situation by using $situation_ratio. This works (nearly) for invoice that was not correctly recorded |
||
| 3907 | // but it may introduces an error for situation invoices that were correctly saved. There is still rounding problem that differs between |
||
| 3908 | // real data we should have stored and result obtained with a compensation. |
||
| 3909 | // It also seems that credit notes on situation invoices are correctly saved (but it depends on the version used in fact). |
||
| 3910 | // For credit notes, we hope to have situation_ratio = 1 so the compensation has no effect to avoid introducing troubles with credit notes. |
||
| 3911 | if (getDolGlobalInt('INVOICE_USE_SITUATION') == 1) { |
||
| 3912 | $total_ttc = $obj->total_ttc * $situation_ratio; |
||
| 3913 | } else { |
||
| 3914 | $total_ttc = $obj->total_ttc; |
||
| 3915 | } |
||
| 3916 | |||
| 3917 | // Move a part of the retained warrenty into the account of warranty |
||
| 3918 | if (getDolGlobalString('INVOICE_USE_RETAINED_WARRANTY') && $obj->retained_warranty > 0) { |
||
| 3919 | $retained_warranty = (float) price2num($total_ttc * $obj->retained_warranty / 100, 'MT'); // Calculate the amount of warrenty for this line (using the percent value) |
||
| 3920 | $tabwarranty[$obj->rowid][$compta_soc] += $retained_warranty; |
||
| 3921 | $total_ttc -= $retained_warranty; |
||
| 3922 | } |
||
| 3923 | |||
| 3924 | $tabttc[$obj->rowid][$compta_soc] += $total_ttc; |
||
| 3925 | $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio; |
||
| 3926 | $tva_npr = ((($obj->info_bits & 1) == 1) ? 1 : 0); |
||
| 3927 | if (!$tva_npr) { // We ignore line if VAT is a NPR |
||
| 3928 | $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio; |
||
| 3929 | } |
||
| 3930 | $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1 * $situation_ratio; |
||
| 3931 | $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2 * $situation_ratio; |
||
| 3932 | |||
| 3933 | $compta_revenuestamp = 'NotDefined'; |
||
| 3934 | if (!empty($revenuestamp)) { |
||
| 3935 | $sqlrevenuestamp = "SELECT accountancy_code_sell FROM " . MAIN_DB_PREFIX . "c_revenuestamp"; |
||
| 3936 | $sqlrevenuestamp .= " WHERE fk_pays = " . ((int) $mysoc->country_id); |
||
| 3937 | $sqlrevenuestamp .= " AND taux = " . ((float) $revenuestamp); |
||
| 3938 | $sqlrevenuestamp .= " AND active = 1"; |
||
| 3939 | $resqlrevenuestamp = $db->query($sqlrevenuestamp); |
||
| 3940 | |||
| 3941 | if ($resqlrevenuestamp) { |
||
| 3942 | $num_rows_revenuestamp = $db->num_rows($resqlrevenuestamp); |
||
| 3943 | if ($num_rows_revenuestamp > 1) { |
||
| 3944 | dol_print_error($db, 'Failed 2 or more lines for the revenue stamp of your country. Check the dictionary of revenue stamp.'); |
||
| 3945 | } else { |
||
| 3946 | $objrevenuestamp = $db->fetch_object($resqlrevenuestamp); |
||
| 3947 | if ($objrevenuestamp) { |
||
| 3948 | $compta_revenuestamp = $objrevenuestamp->accountancy_code_sell; |
||
| 3949 | } |
||
| 3950 | } |
||
| 3951 | } |
||
| 3952 | } |
||
| 3953 | |||
| 3954 | if (empty($tabrevenuestamp[$obj->rowid][$compta_revenuestamp]) && !empty($revenuestamp)) { |
||
| 3955 | // The revenue stamp was never seen for this invoice id=$obj->rowid |
||
| 3956 | $tabttc[$obj->rowid][$compta_soc] += $obj->revenuestamp; |
||
| 3957 | $tabrevenuestamp[$obj->rowid][$compta_revenuestamp] = $obj->revenuestamp; |
||
| 3958 | } |
||
| 3959 | |||
| 3960 | $tabcompany[$obj->rowid] = array( |
||
| 3961 | 'id' => $obj->socid, |
||
| 3962 | 'name' => $obj->name, |
||
| 3963 | 'code_client' => $obj->code_client, |
||
| 3964 | 'code_compta' => $compta_soc |
||
| 3965 | ); |
||
| 3966 | |||
| 3967 | $i++; |
||
| 3968 | } |
||
| 3969 | |||
| 3970 | // After the loop on each line |
||
| 3971 | } else { |
||
| 3972 | dol_print_error($db); |
||
| 3973 | } |
||
| 3974 | |||
| 3975 | // Check for too many invoices first. |
||
| 3976 | if (count($tabfac) > 10000) { |
||
| 3977 | $error++; |
||
| 3978 | setEventMessages("TooManyInvoicesToProcessPleaseUseAMoreSelectiveFilter", null, 'errors'); |
||
| 3979 | } |
||
| 3980 | |||
| 3981 | $errorforinvoice = array(); |
||
| 3982 | |||
| 3983 | /* |
||
| 3984 | // Old way, 1 query for each invoice |
||
| 3985 | // Loop on all invoices to detect lines without binded code (fk_code_ventilation <= 0) |
||
| 3986 | foreach ($tabfac as $key => $val) { // Loop on each invoice |
||
| 3987 | $sql = "SELECT COUNT(fd.rowid) as nb"; |
||
| 3988 | $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd"; |
||
| 3989 | $sql .= " WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0"; |
||
| 3990 | $sql .= " AND fd.total_ttc <> 0 AND fk_facture = ".((int) $key); |
||
| 3991 | $resql = $db->query($sql); |
||
| 3992 | if ($resql) { |
||
| 3993 | $obj = $db->fetch_object($resql); |
||
| 3994 | if ($obj->nb > 0) { |
||
| 3995 | $errorforinvoice[$key] = 'somelinesarenotbound'; |
||
| 3996 | } |
||
| 3997 | } else { |
||
| 3998 | dol_print_error($db); |
||
| 3999 | } |
||
| 4000 | } |
||
| 4001 | */ |
||
| 4002 | // New way, single query, load all unbound lines |
||
| 4003 | |||
| 4004 | $sql = " |
||
| 4005 | SELECT |
||
| 4006 | fk_facture, |
||
| 4007 | COUNT(fd.rowid) as nb |
||
| 4008 | FROM |
||
| 4009 | " . MAIN_DB_PREFIX . "facturedet as fd |
||
| 4010 | WHERE |
||
| 4011 | fd.product_type <= 2 |
||
| 4012 | AND fd.fk_code_ventilation <= 0 |
||
| 4013 | AND fd.total_ttc <> 0 |
||
| 4014 | AND fk_facture IN (" . $db->sanitize(implode(",", array_keys($tabfac))) . ") |
||
| 4015 | GROUP BY fk_facture |
||
| 4016 | "; |
||
| 4017 | $resql = $db->query($sql); |
||
| 4018 | |||
| 4019 | $num = $db->num_rows($resql); |
||
| 4020 | $i = 0; |
||
| 4021 | while ($i < $num) { |
||
| 4022 | $obj = $db->fetch_object($resql); |
||
| 4023 | if ($obj->nb > 0) { |
||
| 4024 | $errorforinvoice[$obj->fk_facture_fourn] = 'somelinesarenotbound'; |
||
| 4025 | } |
||
| 4026 | $i++; |
||
| 4027 | } |
||
| 4028 | //var_dump($errorforinvoice);exit; |
||
| 4029 | |||
| 4030 | // Bookkeeping Write |
||
| 4031 | if ($action == 'writebookkeeping' && !$error) { |
||
| 4032 | $now = dol_now(); |
||
| 4033 | $error = 0; |
||
| 4034 | |||
| 4035 | $companystatic = new Societe($db); |
||
| 4036 | $invoicestatic = new Facture($db); |
||
| 4037 | $accountingaccountcustomer = new AccountingAccount($db); |
||
| 4038 | |||
| 4039 | $accountingaccountcustomer->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), true); |
||
| 4040 | |||
| 4041 | $accountingaccountcustomerwarranty = new AccountingAccount($db); |
||
| 4042 | |||
| 4043 | $accountingaccountcustomerwarranty->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY'), true); |
||
| 4044 | |||
| 4045 | foreach ($tabfac as $key => $val) { // Loop on each invoice |
||
| 4046 | $errorforline = 0; |
||
| 4047 | |||
| 4048 | $totalcredit = 0; |
||
| 4049 | $totaldebit = 0; |
||
| 4050 | |||
| 4051 | $db->begin(); |
||
| 4052 | |||
| 4053 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 4054 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 4055 | $companystatic->code_compta = $tabcompany[$key]['code_compta']; |
||
| 4056 | $companystatic->code_compta_client = $tabcompany[$key]['code_compta']; |
||
| 4057 | $companystatic->code_client = $tabcompany[$key]['code_client']; |
||
| 4058 | $companystatic->client = 3; |
||
| 4059 | |||
| 4060 | $invoicestatic->id = $key; |
||
| 4061 | $invoicestatic->ref = (string) $val["ref"]; |
||
| 4062 | $invoicestatic->type = $val["type"]; |
||
| 4063 | $invoicestatic->close_code = $val["close_code"]; |
||
| 4064 | |||
| 4065 | $date = dol_print_date($val["date"], 'day'); |
||
| 4066 | |||
| 4067 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 4068 | $replacedinvoice = 0; |
||
| 4069 | if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) { |
||
| 4070 | $replacedinvoice = 1; |
||
| 4071 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 4072 | if ($alreadydispatched) { |
||
| 4073 | $replacedinvoice = 2; |
||
| 4074 | } |
||
| 4075 | } |
||
| 4076 | |||
| 4077 | // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted) |
||
| 4078 | if ($replacedinvoice == 1) { |
||
| 4079 | $db->rollback(); |
||
| 4080 | continue; |
||
| 4081 | } |
||
| 4082 | |||
| 4083 | // Error if some lines are not binded/ready to be journalized |
||
| 4084 | if ($errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 4085 | $error++; |
||
| 4086 | $errorforline++; |
||
| 4087 | setEventMessages($langs->trans('ErrorInvoiceContainsLinesNotYetBounded', $val['ref']), null, 'errors'); |
||
| 4088 | } |
||
| 4089 | |||
| 4090 | // Warranty |
||
| 4091 | if (!$errorforline) { |
||
| 4092 | if (is_array($tabwarranty[$key])) { |
||
| 4093 | foreach ($tabwarranty[$key] as $k => $mt) { |
||
| 4094 | $bookkeeping = new BookKeeping($db); |
||
| 4095 | $bookkeeping->doc_date = $val["date"]; |
||
| 4096 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 4097 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 4098 | $bookkeeping->date_creation = $now; |
||
| 4099 | $bookkeeping->doc_type = 'customer_invoice'; |
||
| 4100 | $bookkeeping->fk_doc = $key; |
||
| 4101 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 4102 | $bookkeeping->thirdparty_code = $companystatic->code_client; |
||
| 4103 | |||
| 4104 | $bookkeeping->subledger_account = $tabcompany[$key]['code_compta']; |
||
| 4105 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 4106 | |||
| 4107 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY'); |
||
| 4108 | $bookkeeping->label_compte = $accountingaccountcustomerwarranty->label; |
||
| 4109 | |||
| 4110 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Retainedwarranty"); |
||
| 4111 | $bookkeeping->montant = $mt; |
||
| 4112 | $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; |
||
| 4113 | $bookkeeping->debit = ($mt >= 0) ? $mt : 0; |
||
| 4114 | $bookkeeping->credit = ($mt < 0) ? -$mt : 0; |
||
| 4115 | $bookkeeping->code_journal = $journal; |
||
| 4116 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 4117 | $bookkeeping->fk_user_author = $user->id; |
||
| 4118 | $bookkeeping->entity = $conf->entity; |
||
| 4119 | |||
| 4120 | $totaldebit += $bookkeeping->debit; |
||
| 4121 | $totalcredit += $bookkeeping->credit; |
||
| 4122 | |||
| 4123 | $result = $bookkeeping->create($user); |
||
| 4124 | if ($result < 0) { |
||
| 4125 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 4126 | $error++; |
||
| 4127 | $errorforline++; |
||
| 4128 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 4129 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 4130 | } else { |
||
| 4131 | $error++; |
||
| 4132 | $errorforline++; |
||
| 4133 | $errorforinvoice[$key] = 'other'; |
||
| 4134 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 4135 | } |
||
| 4136 | } |
||
| 4137 | } |
||
| 4138 | } |
||
| 4139 | } |
||
| 4140 | |||
| 4141 | // Thirdparty |
||
| 4142 | if (!$errorforline) { |
||
| 4143 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 4144 | $bookkeeping = new BookKeeping($db); |
||
| 4145 | $bookkeeping->doc_date = $val["date"]; |
||
| 4146 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 4147 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 4148 | $bookkeeping->date_creation = $now; |
||
| 4149 | $bookkeeping->doc_type = 'customer_invoice'; |
||
| 4150 | $bookkeeping->fk_doc = $key; |
||
| 4151 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 4152 | $bookkeeping->thirdparty_code = $companystatic->code_client; |
||
| 4153 | |||
| 4154 | $bookkeeping->subledger_account = $tabcompany[$key]['code_compta']; |
||
| 4155 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 4156 | |||
| 4157 | $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
||
| 4158 | $bookkeeping->label_compte = $accountingaccountcustomer->label; |
||
| 4159 | |||
| 4160 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("SubledgerAccount"); |
||
| 4161 | $bookkeeping->montant = $mt; |
||
| 4162 | $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; |
||
| 4163 | $bookkeeping->debit = ($mt >= 0) ? $mt : 0; |
||
| 4164 | $bookkeeping->credit = ($mt < 0) ? -$mt : 0; |
||
| 4165 | $bookkeeping->code_journal = $journal; |
||
| 4166 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 4167 | $bookkeeping->fk_user_author = $user->id; |
||
| 4168 | $bookkeeping->entity = $conf->entity; |
||
| 4169 | |||
| 4170 | $totaldebit += $bookkeeping->debit; |
||
| 4171 | $totalcredit += $bookkeeping->credit; |
||
| 4172 | |||
| 4173 | $result = $bookkeeping->create($user); |
||
| 4174 | if ($result < 0) { |
||
| 4175 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 4176 | $error++; |
||
| 4177 | $errorforline++; |
||
| 4178 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 4179 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 4180 | } else { |
||
| 4181 | $error++; |
||
| 4182 | $errorforline++; |
||
| 4183 | $errorforinvoice[$key] = 'other'; |
||
| 4184 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 4185 | } |
||
| 4186 | } else { |
||
| 4187 | if (getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && getDolGlobalInt('ACCOUNTING_ENABLE_AUTOLETTERING')) { |
||
| 4188 | require_once DOL_DOCUMENT_ROOT . '/accountancy/class/lettering.class.php'; |
||
| 4189 | $lettering_static = new Lettering($db); |
||
| 4190 | |||
| 4191 | $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id)); |
||
| 4192 | } |
||
| 4193 | } |
||
| 4194 | } |
||
| 4195 | } |
||
| 4196 | |||
| 4197 | // Product / Service |
||
| 4198 | if (!$errorforline) { |
||
| 4199 | foreach ($tabht[$key] as $k => $mt) { |
||
| 4200 | $resultfetch = $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
||
| 4201 | $label_account = $accountingaccount->label; |
||
| 4202 | |||
| 4203 | // get compte id and label |
||
| 4204 | if ($resultfetch > 0) { |
||
| 4205 | $bookkeeping = new BookKeeping($db); |
||
| 4206 | $bookkeeping->doc_date = $val["date"]; |
||
| 4207 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 4208 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 4209 | $bookkeeping->date_creation = $now; |
||
| 4210 | $bookkeeping->doc_type = 'customer_invoice'; |
||
| 4211 | $bookkeeping->fk_doc = $key; |
||
| 4212 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 4213 | $bookkeeping->thirdparty_code = $companystatic->code_client; |
||
| 4214 | |||
| 4215 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_USE_AUXILIARY_ON_DEPOSIT')) { |
||
| 4216 | if ($k == getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT')) { |
||
| 4217 | $bookkeeping->subledger_account = $tabcompany[$key]['code_compta']; |
||
| 4218 | $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
||
| 4219 | } else { |
||
| 4220 | $bookkeeping->subledger_account = ''; |
||
| 4221 | $bookkeeping->subledger_label = ''; |
||
| 4222 | } |
||
| 4223 | } else { |
||
| 4224 | $bookkeeping->subledger_account = ''; |
||
| 4225 | $bookkeeping->subledger_label = ''; |
||
| 4226 | } |
||
| 4227 | |||
| 4228 | $bookkeeping->numero_compte = $k; |
||
| 4229 | $bookkeeping->label_compte = $label_account; |
||
| 4230 | |||
| 4231 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref . ' - ' . $label_account; |
||
| 4232 | $bookkeeping->montant = $mt; |
||
| 4233 | $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
||
| 4234 | $bookkeeping->debit = ($mt < 0) ? -$mt : 0; |
||
| 4235 | $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
||
| 4236 | $bookkeeping->code_journal = $journal; |
||
| 4237 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 4238 | $bookkeeping->fk_user_author = $user->id; |
||
| 4239 | $bookkeeping->entity = $conf->entity; |
||
| 4240 | |||
| 4241 | $totaldebit += $bookkeeping->debit; |
||
| 4242 | $totalcredit += $bookkeeping->credit; |
||
| 4243 | |||
| 4244 | $result = $bookkeeping->create($user); |
||
| 4245 | if ($result < 0) { |
||
| 4246 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 4247 | $error++; |
||
| 4248 | $errorforline++; |
||
| 4249 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 4250 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 4251 | } else { |
||
| 4252 | $error++; |
||
| 4253 | $errorforline++; |
||
| 4254 | $errorforinvoice[$key] = 'other'; |
||
| 4255 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 4256 | } |
||
| 4257 | } |
||
| 4258 | } |
||
| 4259 | } |
||
| 4260 | } |
||
| 4261 | |||
| 4262 | // VAT |
||
| 4263 | if (!$errorforline) { |
||
| 4264 | $listoftax = array(0, 1, 2); |
||
| 4265 | foreach ($listoftax as $numtax) { |
||
| 4266 | $arrayofvat = $tabtva; |
||
| 4267 | if ($numtax == 1) { |
||
| 4268 | $arrayofvat = $tablocaltax1; |
||
| 4269 | } |
||
| 4270 | if ($numtax == 2) { |
||
| 4271 | $arrayofvat = $tablocaltax2; |
||
| 4272 | } |
||
| 4273 | |||
| 4274 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 4275 | if ($mt) { |
||
| 4276 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache for label |
||
| 4277 | $label_account = $accountingaccount->label; |
||
| 4278 | |||
| 4279 | $bookkeeping = new BookKeeping($db); |
||
| 4280 | $bookkeeping->doc_date = $val["date"]; |
||
| 4281 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 4282 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 4283 | $bookkeeping->date_creation = $now; |
||
| 4284 | $bookkeeping->doc_type = 'customer_invoice'; |
||
| 4285 | $bookkeeping->fk_doc = $key; |
||
| 4286 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 4287 | $bookkeeping->thirdparty_code = $companystatic->code_client; |
||
| 4288 | |||
| 4289 | $bookkeeping->subledger_account = ''; |
||
| 4290 | $bookkeeping->subledger_label = ''; |
||
| 4291 | |||
| 4292 | $bookkeeping->numero_compte = $k; |
||
| 4293 | $bookkeeping->label_compte = $label_account; |
||
| 4294 | |||
| 4295 | |||
| 4296 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref; |
||
| 4297 | $tmpvatrate = (empty($def_tva[$key][$k]) ? (empty($arrayofvat[$key][$k]) ? '' : $arrayofvat[$key][$k]) : implode(', ', $def_tva[$key][$k])); |
||
| 4298 | $bookkeeping->label_operation .= ' - ' . $langs->trans("Taxes") . ' ' . $tmpvatrate . ' %'; |
||
| 4299 | $bookkeeping->label_operation .= ($numtax ? ' - Localtax ' . $numtax : ''); |
||
| 4300 | |||
| 4301 | $bookkeeping->montant = $mt; |
||
| 4302 | $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
||
| 4303 | $bookkeeping->debit = ($mt < 0) ? -$mt : 0; |
||
| 4304 | $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
||
| 4305 | $bookkeeping->code_journal = $journal; |
||
| 4306 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 4307 | $bookkeeping->fk_user_author = $user->id; |
||
| 4308 | $bookkeeping->entity = $conf->entity; |
||
| 4309 | |||
| 4310 | $totaldebit += $bookkeeping->debit; |
||
| 4311 | $totalcredit += $bookkeeping->credit; |
||
| 4312 | |||
| 4313 | $result = $bookkeeping->create($user); |
||
| 4314 | if ($result < 0) { |
||
| 4315 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 4316 | $error++; |
||
| 4317 | $errorforline++; |
||
| 4318 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 4319 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 4320 | } else { |
||
| 4321 | $error++; |
||
| 4322 | $errorforline++; |
||
| 4323 | $errorforinvoice[$key] = 'other'; |
||
| 4324 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 4325 | } |
||
| 4326 | } |
||
| 4327 | } |
||
| 4328 | } |
||
| 4329 | } |
||
| 4330 | } |
||
| 4331 | |||
| 4332 | // Revenue stamp |
||
| 4333 | if (!$errorforline) { |
||
| 4334 | if (is_array($tabrevenuestamp[$key])) { |
||
| 4335 | foreach ($tabrevenuestamp[$key] as $k => $mt) { |
||
| 4336 | if ($mt) { |
||
| 4337 | $accountingaccount->fetch(null, $k, true); // TODO Use a cache for label |
||
| 4338 | $label_account = $accountingaccount->label; |
||
| 4339 | |||
| 4340 | $bookkeeping = new BookKeeping($db); |
||
| 4341 | $bookkeeping->doc_date = $val["date"]; |
||
| 4342 | $bookkeeping->date_lim_reglement = $val["datereg"]; |
||
| 4343 | $bookkeeping->doc_ref = $val["ref"]; |
||
| 4344 | $bookkeeping->date_creation = $now; |
||
| 4345 | $bookkeeping->doc_type = 'customer_invoice'; |
||
| 4346 | $bookkeeping->fk_doc = $key; |
||
| 4347 | $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add |
||
| 4348 | $bookkeeping->thirdparty_code = $companystatic->code_client; |
||
| 4349 | |||
| 4350 | $bookkeeping->subledger_account = ''; |
||
| 4351 | $bookkeeping->subledger_label = ''; |
||
| 4352 | |||
| 4353 | $bookkeeping->numero_compte = $k; |
||
| 4354 | $bookkeeping->label_compte = $label_account; |
||
| 4355 | |||
| 4356 | $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("RevenueStamp"); |
||
| 4357 | $bookkeeping->montant = $mt; |
||
| 4358 | $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
||
| 4359 | $bookkeeping->debit = ($mt < 0) ? -$mt : 0; |
||
| 4360 | $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
||
| 4361 | $bookkeeping->code_journal = $journal; |
||
| 4362 | $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
||
| 4363 | $bookkeeping->fk_user_author = $user->id; |
||
| 4364 | $bookkeeping->entity = $conf->entity; |
||
| 4365 | |||
| 4366 | $totaldebit += $bookkeeping->debit; |
||
| 4367 | $totalcredit += $bookkeeping->credit; |
||
| 4368 | |||
| 4369 | $result = $bookkeeping->create($user); |
||
| 4370 | if ($result < 0) { |
||
| 4371 | if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
||
| 4372 | $error++; |
||
| 4373 | $errorforline++; |
||
| 4374 | $errorforinvoice[$key] = 'alreadyjournalized'; |
||
| 4375 | //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
||
| 4376 | } else { |
||
| 4377 | $error++; |
||
| 4378 | $errorforline++; |
||
| 4379 | $errorforinvoice[$key] = 'other'; |
||
| 4380 | setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
||
| 4381 | } |
||
| 4382 | } |
||
| 4383 | } |
||
| 4384 | } |
||
| 4385 | } |
||
| 4386 | } |
||
| 4387 | |||
| 4388 | // Protection against a bug on lines before |
||
| 4389 | if (!$errorforline && (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT'))) { |
||
| 4390 | $error++; |
||
| 4391 | $errorforline++; |
||
| 4392 | $errorforinvoice[$key] = 'amountsnotbalanced'; |
||
| 4393 | setEventMessages('We Tried to insert a non balanced transaction in book for ' . $invoicestatic->ref . '. Canceled. Surely a bug.', null, 'errors'); |
||
| 4394 | } |
||
| 4395 | |||
| 4396 | if (!$errorforline) { |
||
| 4397 | $db->commit(); |
||
| 4398 | } else { |
||
| 4399 | $db->rollback(); |
||
| 4400 | |||
| 4401 | if ($error >= 10) { |
||
| 4402 | setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors'); |
||
| 4403 | break; // Break in the foreach |
||
| 4404 | } |
||
| 4405 | } |
||
| 4406 | } |
||
| 4407 | |||
| 4408 | $tabpay = $tabfac; |
||
| 4409 | |||
| 4410 | if (empty($error) && count($tabpay) > 0) { |
||
| 4411 | setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
||
| 4412 | } elseif (count($tabpay) == $error) { |
||
| 4413 | setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
||
| 4414 | } else { |
||
| 4415 | setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
||
| 4416 | } |
||
| 4417 | |||
| 4418 | $action = ''; |
||
| 4419 | |||
| 4420 | // Must reload data, so we make a redirect |
||
| 4421 | if (count($tabpay) != $error) { |
||
| 4422 | $param = 'id_journal=' . $id_journal; |
||
| 4423 | $param .= '&date_startday=' . $date_startday; |
||
| 4424 | $param .= '&date_startmonth=' . $date_startmonth; |
||
| 4425 | $param .= '&date_startyear=' . $date_startyear; |
||
| 4426 | $param .= '&date_endday=' . $date_endday; |
||
| 4427 | $param .= '&date_endmonth=' . $date_endmonth; |
||
| 4428 | $param .= '&date_endyear=' . $date_endyear; |
||
| 4429 | $param .= '&in_bookkeeping=' . $in_bookkeeping; |
||
| 4430 | header("Location: " . $_SERVER['PHP_SELF'] . ($param ? '?' . $param : '')); |
||
| 4431 | exit; |
||
| 4432 | } |
||
| 4433 | } |
||
| 4434 | |||
| 4435 | |||
| 4436 | |||
| 4437 | /* |
||
| 4438 | * View |
||
| 4439 | */ |
||
| 4440 | |||
| 4441 | $form = new Form($db); |
||
| 4442 | |||
| 4443 | // Export |
||
| 4444 | if ($action == 'exportcsv' && !$error) { // ISO and not UTF8 ! |
||
| 4445 | // Note that to have the button to get this feature enabled, you must enable ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL |
||
| 4446 | $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
||
| 4447 | |||
| 4448 | $filename = 'journal'; |
||
| 4449 | $type_export = 'journal'; |
||
| 4450 | include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php'; |
||
| 4451 | |||
| 4452 | $companystatic = new Client($db); |
||
| 4453 | $invoicestatic = new Facture($db); |
||
| 4454 | |||
| 4455 | foreach ($tabfac as $key => $val) { |
||
| 4456 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 4457 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 4458 | $companystatic->code_compta = $tabcompany[$key]['code_compta']; // deprecated |
||
| 4459 | $companystatic->code_compta_client = $tabcompany[$key]['code_compta']; |
||
| 4460 | $companystatic->code_client = $tabcompany[$key]['code_client']; |
||
| 4461 | $companystatic->client = 3; |
||
| 4462 | |||
| 4463 | $invoicestatic->id = $key; |
||
| 4464 | $invoicestatic->ref = (string) $val["ref"]; |
||
| 4465 | $invoicestatic->type = $val["type"]; |
||
| 4466 | $invoicestatic->close_code = $val["close_code"]; |
||
| 4467 | |||
| 4468 | $date = dol_print_date($val["date"], 'day'); |
||
| 4469 | |||
| 4470 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 4471 | $replacedinvoice = 0; |
||
| 4472 | if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) { |
||
| 4473 | $replacedinvoice = 1; |
||
| 4474 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 4475 | if ($alreadydispatched) { |
||
| 4476 | $replacedinvoice = 2; |
||
| 4477 | } |
||
| 4478 | } |
||
| 4479 | |||
| 4480 | // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted) |
||
| 4481 | if ($replacedinvoice == 1) { |
||
| 4482 | continue; |
||
| 4483 | } |
||
| 4484 | |||
| 4485 | // Warranty |
||
| 4486 | foreach ($tabwarranty[$key] as $k => $mt) { |
||
| 4487 | //if ($mt) { |
||
| 4488 | print '"' . $key . '"' . $sep; |
||
| 4489 | print '"' . $date . '"' . $sep; |
||
| 4490 | print '"' . $val["ref"] . '"' . $sep; |
||
| 4491 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4492 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 4493 | print '"' . length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY')) . '"' . $sep; |
||
| 4494 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 4495 | print '"' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 4496 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Retainedwarranty") . '"' . $sep; |
||
| 4497 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 4498 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 4499 | print '"' . $journal . '"'; |
||
| 4500 | print "\n"; |
||
| 4501 | //} |
||
| 4502 | } |
||
| 4503 | |||
| 4504 | // Third party |
||
| 4505 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 4506 | //if ($mt) { |
||
| 4507 | print '"' . $key . '"' . $sep; |
||
| 4508 | print '"' . $date . '"' . $sep; |
||
| 4509 | print '"' . $val["ref"] . '"' . $sep; |
||
| 4510 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4511 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 4512 | print '"' . length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER')) . '"' . $sep; |
||
| 4513 | print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep; |
||
| 4514 | print '"' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 4515 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Thirdparty") . '"' . $sep; |
||
| 4516 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 4517 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 4518 | print '"' . $journal . '"'; |
||
| 4519 | print "\n"; |
||
| 4520 | //} |
||
| 4521 | } |
||
| 4522 | |||
| 4523 | // Product / Service |
||
| 4524 | foreach ($tabht[$key] as $k => $mt) { |
||
| 4525 | $accountingaccount = new AccountingAccount($db); |
||
| 4526 | $accountingaccount->fetch(null, $k, true); |
||
| 4527 | //if ($mt) { |
||
| 4528 | print '"' . $key . '"' . $sep; |
||
| 4529 | print '"' . $date . '"' . $sep; |
||
| 4530 | print '"' . $val["ref"] . '"' . $sep; |
||
| 4531 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4532 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4533 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4534 | print '""' . $sep; |
||
| 4535 | print '"' . mb_convert_encoding(dol_trunc($accountingaccount->label, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4536 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . dol_trunc($accountingaccount->label, 32) . '"' . $sep; |
||
| 4537 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 4538 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 4539 | print '"' . $journal . '"'; |
||
| 4540 | print "\n"; |
||
| 4541 | //} |
||
| 4542 | } |
||
| 4543 | |||
| 4544 | // VAT |
||
| 4545 | $listoftax = array(0, 1, 2); |
||
| 4546 | foreach ($listoftax as $numtax) { |
||
| 4547 | $arrayofvat = $tabtva; |
||
| 4548 | if ($numtax == 1) { |
||
| 4549 | $arrayofvat = $tablocaltax1; |
||
| 4550 | } |
||
| 4551 | if ($numtax == 2) { |
||
| 4552 | $arrayofvat = $tablocaltax2; |
||
| 4553 | } |
||
| 4554 | |||
| 4555 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 4556 | if ($mt) { |
||
| 4557 | print '"' . $key . '"' . $sep; |
||
| 4558 | print '"' . $date . '"' . $sep; |
||
| 4559 | print '"' . $val["ref"] . '"' . $sep; |
||
| 4560 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4561 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4562 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4563 | print '""' . $sep; |
||
| 4564 | print '"' . $langs->trans("VAT") . ' - ' . implode(', ', $def_tva[$key][$k]) . ' %"' . $sep; |
||
| 4565 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("VAT") . implode(', ', $def_tva[$key][$k]) . ' %' . ($numtax ? ' - Localtax ' . $numtax : '') . '"' . $sep; |
||
| 4566 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 4567 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 4568 | print '"' . $journal . '"'; |
||
| 4569 | print "\n"; |
||
| 4570 | } |
||
| 4571 | } |
||
| 4572 | } |
||
| 4573 | |||
| 4574 | // Revenue stamp |
||
| 4575 | foreach ($tabrevenuestamp[$key] as $k => $mt) { |
||
| 4576 | //if ($mt) { |
||
| 4577 | print '"' . $key . '"' . $sep; |
||
| 4578 | print '"' . $date . '"' . $sep; |
||
| 4579 | print '"' . $val["ref"] . '"' . $sep; |
||
| 4580 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 32), 'ISO-8859-1') . '"' . $sep; |
||
| 4581 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4582 | print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep; |
||
| 4583 | print '""' . $sep; |
||
| 4584 | print '"' . $langs->trans("RevenueStamp") . '"' . $sep; |
||
| 4585 | print '"' . mb_convert_encoding(dol_trunc($companystatic->name, 16), 'ISO-8859-1') . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("RevenueStamp") . '"' . $sep; |
||
| 4586 | print '"' . ($mt < 0 ? price(-$mt) : '') . '"' . $sep; |
||
| 4587 | print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep; |
||
| 4588 | print '"' . $journal . '"'; |
||
| 4589 | print "\n"; |
||
| 4590 | //} |
||
| 4591 | } |
||
| 4592 | } |
||
| 4593 | } |
||
| 4594 | |||
| 4595 | |||
| 4596 | |||
| 4597 | if (empty($action) || $action == 'view') { |
||
| 4598 | $title = $langs->trans("GenerationOfAccountingEntries") . ' - ' . $accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
||
| 4599 | |||
| 4600 | llxHeader('', dol_string_nohtmltag($title)); |
||
| 4601 | |||
| 4602 | $nom = $title; |
||
| 4603 | $nomlink = ''; |
||
| 4604 | $periodlink = ''; |
||
| 4605 | $exportlink = ''; |
||
| 4606 | $builddate = dol_now(); |
||
| 4607 | $description = $langs->trans("DescJournalOnlyBindedVisible") . '<br>'; |
||
| 4608 | if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { |
||
| 4609 | $description .= $langs->trans("DepositsAreNotIncluded"); |
||
| 4610 | } else { |
||
| 4611 | $description .= $langs->trans("DepositsAreIncluded"); |
||
| 4612 | } |
||
| 4613 | |||
| 4614 | $listofchoices = array('notyet' => $langs->trans("NotYetInGeneralLedger"), 'already' => $langs->trans("AlreadyInGeneralLedger")); |
||
| 4615 | $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0) . ' - ' . $form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
||
| 4616 | $period .= ' - ' . $langs->trans("JournalizationInLedgerStatus") . ' ' . $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
||
| 4617 | |||
| 4618 | $varlink = 'id_journal=' . $id_journal; |
||
| 4619 | |||
| 4620 | journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink); |
||
| 4621 | |||
| 4622 | if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
||
| 4623 | // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
||
| 4624 | // Fiscal period test |
||
| 4625 | $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "accounting_fiscalyear WHERE entity = " . ((int) $conf->entity); |
||
| 4626 | $resql = $db->query($sql); |
||
| 4627 | if ($resql) { |
||
| 4628 | $obj = $db->fetch_object($resql); |
||
| 4629 | if ($obj->nb == 0) { |
||
| 4630 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("TheFiscalPeriodIsNotDefined"); |
||
| 4631 | $desc = ' : ' . $langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
||
| 4632 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("FiscalPeriod") . '</strong>', $desc); |
||
| 4633 | print $desc; |
||
| 4634 | print '</div>'; |
||
| 4635 | } |
||
| 4636 | } else { |
||
| 4637 | dol_print_error($db); |
||
| 4638 | } |
||
| 4639 | } |
||
| 4640 | |||
| 4641 | // Button to write into Ledger |
||
| 4642 | $acctCustomerNotConfigured = in_array(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), ['','-1']); |
||
| 4643 | if ($acctCustomerNotConfigured) { |
||
| 4644 | print '<br><div class="warning">' . img_warning() . ' ' . $langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
||
| 4645 | $desc = ' : ' . $langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
||
| 4646 | $desc = str_replace('{link}', '<strong>' . $langs->transnoentitiesnoconv("MenuAccountancy") . '-' . $langs->transnoentitiesnoconv("Setup") . "-" . $langs->transnoentitiesnoconv("MenuDefaultAccounts") . '</strong>', $desc); |
||
| 4647 | print $desc; |
||
| 4648 | print '</div>'; |
||
| 4649 | } |
||
| 4650 | print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
||
| 4651 | if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
||
| 4652 | print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />'; |
||
| 4653 | } |
||
| 4654 | if ($acctCustomerNotConfigured) { |
||
| 4655 | print '<input type="button" class="butActionRefused classfortooltip" title="' . dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")) . '" value="' . $langs->trans("WriteBookKeeping") . '" />'; |
||
| 4656 | } else { |
||
| 4657 | if ($in_bookkeeping == 'notyet') { |
||
| 4658 | print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />'; |
||
| 4659 | } else { |
||
| 4660 | print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>'; |
||
| 4661 | } |
||
| 4662 | } |
||
| 4663 | print '</div>'; |
||
| 4664 | |||
| 4665 | // TODO Avoid using js. We can use a direct link with $param |
||
| 4666 | print ' |
||
| 4667 | <script type="text/javascript"> |
||
| 4668 | function launch_export() { |
||
| 4669 | $("div.fiche form input[name=\"action\"]").val("exportcsv"); |
||
| 4670 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 4671 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 4672 | } |
||
| 4673 | function writebookkeeping() { |
||
| 4674 | console.log("click on writebookkeeping"); |
||
| 4675 | $("div.fiche form input[name=\"action\"]").val("writebookkeeping"); |
||
| 4676 | $("div.fiche form input[type=\"submit\"]").click(); |
||
| 4677 | $("div.fiche form input[name=\"action\"]").val(""); |
||
| 4678 | } |
||
| 4679 | </script>'; |
||
| 4680 | |||
| 4681 | /* |
||
| 4682 | * Show result array |
||
| 4683 | */ |
||
| 4684 | print '<br>'; |
||
| 4685 | |||
| 4686 | print '<div class="div-table-responsive">'; |
||
| 4687 | print "<table class=\"noborder\" width=\"100%\">"; |
||
| 4688 | print "<tr class=\"liste_titre\">"; |
||
| 4689 | print "<td>" . $langs->trans("Date") . "</td>"; |
||
| 4690 | print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>"; |
||
| 4691 | print "<td>" . $langs->trans("AccountAccounting") . "</td>"; |
||
| 4692 | print "<td>" . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 4693 | print "<td>" . $langs->trans("LabelOperation") . "</td>"; |
||
| 4694 | print '<td class="center">' . $langs->trans("AccountingDebit") . "</td>"; |
||
| 4695 | print '<td class="center">' . $langs->trans("AccountingCredit") . "</td>"; |
||
| 4696 | print "</tr>\n"; |
||
| 4697 | |||
| 4698 | $i = 0; |
||
| 4699 | |||
| 4700 | $companystatic = new Client($db); |
||
| 4701 | $invoicestatic = new Facture($db); |
||
| 4702 | |||
| 4703 | foreach ($tabfac as $key => $val) { |
||
| 4704 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 4705 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 4706 | $companystatic->code_compta = $tabcompany[$key]['code_compta']; |
||
| 4707 | $companystatic->code_compta_client = $tabcompany[$key]['code_compta']; |
||
| 4708 | $companystatic->code_client = $tabcompany[$key]['code_client']; |
||
| 4709 | $companystatic->client = 3; |
||
| 4710 | |||
| 4711 | $invoicestatic->id = $key; |
||
| 4712 | $invoicestatic->ref = (string) $val["ref"]; |
||
| 4713 | $invoicestatic->type = $val["type"]; |
||
| 4714 | $invoicestatic->close_code = $val["close_code"]; |
||
| 4715 | |||
| 4716 | $date = dol_print_date($val["date"], 'day'); |
||
| 4717 | |||
| 4718 | // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched |
||
| 4719 | $replacedinvoice = 0; |
||
| 4720 | if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) { |
||
| 4721 | $replacedinvoice = 1; |
||
| 4722 | $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping. |
||
| 4723 | if ($alreadydispatched) { |
||
| 4724 | $replacedinvoice = 2; |
||
| 4725 | } |
||
| 4726 | } |
||
| 4727 | |||
| 4728 | // If not already into bookkeeping, we won't add it, if yes, add the counterpart ???. |
||
| 4729 | if ($replacedinvoice == 1) { |
||
| 4730 | print '<tr class="oddeven">'; |
||
| 4731 | print "<!-- Replaced invoice -->"; |
||
| 4732 | print "<td>" . $date . "</td>"; |
||
| 4733 | print "<td><strike>" . $invoicestatic->getNomUrl(1) . "</strike></td>"; |
||
| 4734 | // Account |
||
| 4735 | print "<td>"; |
||
| 4736 | print $langs->trans("Replaced"); |
||
| 4737 | print '</td>'; |
||
| 4738 | // Subledger account |
||
| 4739 | print "<td>"; |
||
| 4740 | print '</td>'; |
||
| 4741 | print "<td>"; |
||
| 4742 | print "</td>"; |
||
| 4743 | print '<td class="right"></td>'; |
||
| 4744 | print '<td class="right"></td>'; |
||
| 4745 | print "</tr>"; |
||
| 4746 | |||
| 4747 | $i++; |
||
| 4748 | continue; |
||
| 4749 | } |
||
| 4750 | if ($errorforinvoice[$key] == 'somelinesarenotbound') { |
||
| 4751 | print '<tr class="oddeven">'; |
||
| 4752 | print "<!-- Some lines are not bound -->"; |
||
| 4753 | print "<td>" . $date . "</td>"; |
||
| 4754 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4755 | // Account |
||
| 4756 | print "<td>"; |
||
| 4757 | print '<span class="error">' . $langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']) . '</span>'; |
||
| 4758 | print '</td>'; |
||
| 4759 | // Subledger account |
||
| 4760 | print "<td>"; |
||
| 4761 | print '</td>'; |
||
| 4762 | print "<td>"; |
||
| 4763 | print "</td>"; |
||
| 4764 | print '<td class="right"></td>'; |
||
| 4765 | print '<td class="right"></td>'; |
||
| 4766 | print "</tr>"; |
||
| 4767 | |||
| 4768 | $i++; |
||
| 4769 | } |
||
| 4770 | |||
| 4771 | // Warranty |
||
| 4772 | if (is_array($tabwarranty[$key])) { |
||
| 4773 | foreach ($tabwarranty[$key] as $k => $mt) { |
||
| 4774 | print '<tr class="oddeven">'; |
||
| 4775 | print "<!-- Thirdparty warranty -->"; |
||
| 4776 | print "<td>" . $date . "</td>"; |
||
| 4777 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4778 | // Account |
||
| 4779 | print "<td>"; |
||
| 4780 | $accountoshow = length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY')); |
||
| 4781 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4782 | print '<span class="error">' . $langs->trans("MainAccountForRetainedWarrantyNotDefined") . '</span>'; |
||
| 4783 | } else { |
||
| 4784 | print $accountoshow; |
||
| 4785 | } |
||
| 4786 | print '</td>'; |
||
| 4787 | // Subledger account |
||
| 4788 | print "<td>"; |
||
| 4789 | $accountoshow = length_accounta($k); |
||
| 4790 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4791 | print '<span class="error">' . $langs->trans("ThirdpartyAccountNotDefined") . '</span>'; |
||
| 4792 | } else { |
||
| 4793 | print $accountoshow; |
||
| 4794 | } |
||
| 4795 | print '</td>'; |
||
| 4796 | print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Retainedwarranty") . "</td>"; |
||
| 4797 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 4798 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 4799 | print "</tr>"; |
||
| 4800 | } |
||
| 4801 | } |
||
| 4802 | |||
| 4803 | // Third party |
||
| 4804 | foreach ($tabttc[$key] as $k => $mt) { |
||
| 4805 | print '<tr class="oddeven">'; |
||
| 4806 | print "<!-- Thirdparty -->"; |
||
| 4807 | print "<td>" . $date . "</td>"; |
||
| 4808 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4809 | // Account |
||
| 4810 | print "<td>"; |
||
| 4811 | $accountoshow = length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER')); |
||
| 4812 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4813 | print '<span class="error">' . $langs->trans("MainAccountForCustomersNotDefined") . '</span>'; |
||
| 4814 | } else { |
||
| 4815 | print $accountoshow; |
||
| 4816 | } |
||
| 4817 | print '</td>'; |
||
| 4818 | // Subledger account |
||
| 4819 | print "<td>"; |
||
| 4820 | $accountoshow = length_accounta($k); |
||
| 4821 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4822 | print '<span class="error">' . $langs->trans("ThirdpartyAccountNotDefined") . '</span>'; |
||
| 4823 | } else { |
||
| 4824 | print $accountoshow; |
||
| 4825 | } |
||
| 4826 | print '</td>'; |
||
| 4827 | print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("SubledgerAccount") . "</td>"; |
||
| 4828 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 4829 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 4830 | print "</tr>"; |
||
| 4831 | |||
| 4832 | $i++; |
||
| 4833 | } |
||
| 4834 | |||
| 4835 | // Product / Service |
||
| 4836 | foreach ($tabht[$key] as $k => $mt) { |
||
| 4837 | $accountingaccount = new AccountingAccount($db); |
||
| 4838 | $accountingaccount->fetch(null, $k, true); |
||
| 4839 | |||
| 4840 | print '<tr class="oddeven">'; |
||
| 4841 | print "<!-- Product -->"; |
||
| 4842 | print "<td>" . $date . "</td>"; |
||
| 4843 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4844 | // Account |
||
| 4845 | print "<td>"; |
||
| 4846 | $accountoshow = length_accountg($k); |
||
| 4847 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4848 | print '<span class="error">' . $langs->trans("ProductNotDefined") . '</span>'; |
||
| 4849 | } else { |
||
| 4850 | print $accountoshow; |
||
| 4851 | } |
||
| 4852 | print "</td>"; |
||
| 4853 | // Subledger account |
||
| 4854 | print "<td>"; |
||
| 4855 | if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_USE_AUXILIARY_ON_DEPOSIT')) { |
||
| 4856 | if ($k == getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER_DEPOSIT')) { |
||
| 4857 | print length_accounta($tabcompany[$key]['code_compta']); |
||
| 4858 | } |
||
| 4859 | } elseif (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4860 | print '<span class="error">' . $langs->trans("ThirdpartyAccountNotDefined") . '</span>'; |
||
| 4861 | } |
||
| 4862 | print '</td>'; |
||
| 4863 | $companystatic->id = $tabcompany[$key]['id']; |
||
| 4864 | $companystatic->name = $tabcompany[$key]['name']; |
||
| 4865 | print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $accountingaccount->label . "</td>"; |
||
| 4866 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 4867 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 4868 | print "</tr>"; |
||
| 4869 | |||
| 4870 | $i++; |
||
| 4871 | } |
||
| 4872 | |||
| 4873 | // VAT |
||
| 4874 | $listoftax = array(0, 1, 2); |
||
| 4875 | foreach ($listoftax as $numtax) { |
||
| 4876 | $arrayofvat = $tabtva; |
||
| 4877 | if ($numtax == 1) { |
||
| 4878 | $arrayofvat = $tablocaltax1; |
||
| 4879 | } |
||
| 4880 | if ($numtax == 2) { |
||
| 4881 | $arrayofvat = $tablocaltax2; |
||
| 4882 | } |
||
| 4883 | |||
| 4884 | // $key is id of invoice |
||
| 4885 | foreach ($arrayofvat[$key] as $k => $mt) { |
||
| 4886 | if ($mt) { |
||
| 4887 | print '<tr class="oddeven">'; |
||
| 4888 | print "<!-- VAT -->"; |
||
| 4889 | print "<td>" . $date . "</td>"; |
||
| 4890 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4891 | // Account |
||
| 4892 | print "<td>"; |
||
| 4893 | $accountoshow = length_accountg($k); |
||
| 4894 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4895 | print '<span class="error">' . $langs->trans("VATAccountNotDefined") . ' (' . $langs->trans("AccountingJournalType2") . ')</span>'; |
||
| 4896 | } else { |
||
| 4897 | print $accountoshow; |
||
| 4898 | } |
||
| 4899 | print "</td>"; |
||
| 4900 | // Subledger account |
||
| 4901 | print "<td>"; |
||
| 4902 | print '</td>'; |
||
| 4903 | print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref; |
||
| 4904 | // $def_tva is array[invoiceid][accountancy_code_sell_of_vat_rate_found][vatrate]=vatrate |
||
| 4905 | //var_dump($arrayofvat[$key]); //var_dump($key); //var_dump($k); |
||
| 4906 | $tmpvatrate = (empty($def_tva[$key][$k]) ? (empty($arrayofvat[$key][$k]) ? '' : $arrayofvat[$key][$k]) : implode(', ', $def_tva[$key][$k])); |
||
| 4907 | print ' - ' . $langs->trans("Taxes") . ' ' . $tmpvatrate . ' %'; |
||
| 4908 | print($numtax ? ' - Localtax ' . $numtax : ''); |
||
| 4909 | print "</td>"; |
||
| 4910 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 4911 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 4912 | print "</tr>"; |
||
| 4913 | |||
| 4914 | $i++; |
||
| 4915 | } |
||
| 4916 | } |
||
| 4917 | } |
||
| 4918 | |||
| 4919 | // Revenue stamp |
||
| 4920 | if (is_array($tabrevenuestamp[$key])) { |
||
| 4921 | foreach ($tabrevenuestamp[$key] as $k => $mt) { |
||
| 4922 | print '<tr class="oddeven">'; |
||
| 4923 | print "<!-- Thirdparty revenuestamp -->"; |
||
| 4924 | print "<td>" . $date . "</td>"; |
||
| 4925 | print "<td>" . $invoicestatic->getNomUrl(1) . "</td>"; |
||
| 4926 | // Account |
||
| 4927 | print "<td>"; |
||
| 4928 | $accountoshow = length_accountg($k); |
||
| 4929 | if (($accountoshow == "") || $accountoshow == 'NotDefined') { |
||
| 4930 | print '<span class="error">' . $langs->trans("MainAccountForRevenueStampSaleNotDefined") . '</span>'; |
||
| 4931 | } else { |
||
| 4932 | print $accountoshow; |
||
| 4933 | } |
||
| 4934 | print '</td>'; |
||
| 4935 | // Subledger account |
||
| 4936 | print "<td>"; |
||
| 4937 | print '</td>'; |
||
| 4938 | print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("RevenueStamp") . "</td>"; |
||
| 4939 | print '<td class="right nowraponall amount">' . ($mt < 0 ? price(-$mt) : '') . "</td>"; |
||
| 4940 | print '<td class="right nowraponall amount">' . ($mt >= 0 ? price($mt) : '') . "</td>"; |
||
| 4941 | print "</tr>"; |
||
| 4942 | } |
||
| 4943 | } |
||
| 4944 | } |
||
| 4945 | |||
| 4946 | if (!$i) { |
||
| 4947 | print '<tr class="oddeven"><td colspan="7"><span class="opacitymedium">' . $langs->trans("NoRecordFound") . '</span></td></tr>'; |
||
| 4948 | } |
||
| 4949 | |||
| 4950 | print "</table>"; |
||
| 4951 | print '</div>'; |
||
| 4952 | |||
| 4953 | // End of page |
||
| 4954 | llxFooter(); |
||
| 4955 | } |
||
| 4956 | |||
| 4957 | $db->close(); |
||
| 4958 | } |
||
| 4959 | |||
| 4960 | /** |
||
| 4961 | * \file htdocs/accountancy/journal/variousjournal.php |
||
| 4962 | * \ingroup Accountancy (Double entries) |
||
| 4963 | * \brief Page of a journal |
||
| 4964 | */ |
||
| 4965 | public function variousjournal() |
||
| 5314 | } |
||
| 5315 | } |
||
| 5316 |