170 public function create($user, $closepaidinvoices = 0, $thirdparty =
null)
172 global $langs,
$conf;
181 $totalamount_converted = 0;
182 $atleastonepaymentnotnull = 0;
184 if ($way ==
'dolibarr') {
185 $amounts = &$this->amounts;
186 $amounts_to_update = &$this->multicurrency_amounts;
188 $amounts = &$this->multicurrency_amounts;
189 $amounts_to_update = &$this->amounts;
192 $currencyofpayment =
'';
193 $currencytxofpayment =
'';
195 foreach ($amounts as $key => $value) {
202 if ($value_converted ===
false) {
204 $this->error = $langs->trans(
'FailedToFoundTheConversionRateForInvoice');
207 if (empty($currencyofpayment)) {
208 $currencyofpayment = $this->multicurrency_code[$key];
210 if ($currencyofpayment != $this->multicurrency_code[$key]) {
212 $this->error =
'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment';
215 if (empty($currencytxofpayment)) {
216 $currencytxofpayment = $this->multicurrency_tx[$key];
219 $totalamount_converted += $value_converted;
220 $amounts_to_update[$key] =
price2num($value_converted,
'MT');
223 $amounts[$key] = $newvalue;
224 $totalamount += $newvalue;
225 if (!empty($newvalue)) {
226 $atleastonepaymentnotnull++;
230 if (!empty($currencyofpayment)) {
232 $bankaccount =
new Account($this->db);
233 $bankaccount->fetch($this->fk_account);
234 $bankcurrencycode = empty($bankaccount->currency_code) ?
$conf->currency : $bankaccount->currency_code;
235 if ($currencyofpayment != $bankcurrencycode && $currencyofpayment !=
$conf->currency && $bankcurrencycode !=
$conf->currency) {
236 $langs->load(
"errors");
237 $this->error = $langs->trans(
'ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode);
243 $totalamount = (float)
price2num($totalamount);
244 $totalamount_converted = (float)
price2num($totalamount_converted);
248 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
252 if ($totalamount != 0) {
253 $ref = $this->
getNextNumRef(is_object($thirdparty) ? $thirdparty :
'');
255 if ($way ==
'dolibarr') {
256 $total = $totalamount;
257 $mtotal = $totalamount_converted;
259 $total = $totalamount_converted;
260 $mtotal = $totalamount;
263 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn (';
264 $sql .=
'ref, entity, datec, datep, amount, multicurrency_amount, fk_paiement, num_paiement, note, fk_user_author, fk_bank)';
265 $sql .=
" VALUES ('".$this->db->escape($ref).
"', ".((int)
$conf->entity).
", '".$this->db->idate($now).
"',";
266 $sql .=
" '".$this->db->idate($this->datepaye).
"', ".((float) $total).
", ".((float) $mtotal).
", ".((int) $this->paiementid).
", '".$this->db->escape($this->num_payment).
"', '".$this->db->escape($this->note_private).
"', ".((int) $user->id).
", 0)";
268 $resql = $this->db->query($sql);
270 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'paiementfourn');
273 foreach ($this->amounts as $key => $amount) {
275 if (is_numeric($amount) && $amount != 0) {
277 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount, multicurrency_amount, multicurrency_code, multicurrency_tx)';
278 $sql .=
" VALUES (".((int) $facid).
", ".((int) $this->
id).
", ".((float) $amount).
', '.((float) $this->multicurrency_amounts[$key]).
', '.($currencyofpayment ?
"'".$this->db->escape($currencyofpayment).
"'" :
'NULL').
', '.(!empty($currencytxofpayment) ? (float) $currencytxofpayment : 1).
')';
279 $resql = $this->db->query($sql);
282 $invoice->fetch($facid);
285 if ($closepaidinvoices) {
286 $paiement = $invoice->getSommePaiement();
287 $creditnotes = $invoice->getSumCreditNotesUsed();
289 $deposits = $invoice->getSumDepositsUsed();
291 $alreadypayed =
price2num($paiement + $creditnotes + $deposits,
'MT');
292 $remaintopay =
price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,
'MT');
293 if ($remaintopay == 0) {
296 $amount_ht = $amount_tva = $amount_ttc = array();
297 $multicurrency_amount_ht = $multicurrency_amount_tva = $multicurrency_amount_ttc = array();
299 @phan-var-force array<string,float> $amount_ht
300 @phan-var-force array<string,float> $amount_tva
301 @phan-var-force array<string,float> $amount_ttc
302 @phan-var-force array<string,float> $multicurrency_amount_ht
303 @phan-var-force array<string,float> $multicurrency_amount_tva
304 @phan-var-force array<string,float> $multicurrency_amount_ttc
308 require_once DOL_DOCUMENT_ROOT .
'/core/class/discount.class.php';
310 $discount->fetch(0, 0, $invoice->id);
311 if (empty($discount->id)) {
312 $discount->discount_type = 1;
313 $discount->description =
'(DEPOSIT)';
314 $discount->fk_soc = $invoice->socid;
315 $discount->socid = $invoice->socid;
316 $discount->fk_invoice_supplier_source = $invoice->id;
320 foreach ($invoice->lines as $line) {
321 if ($line->total_ht != 0) {
322 if (!array_key_exists($line->tva_tx, $amount_ht)) {
323 $amount_ht[$line->tva_tx] = 0.0;
324 $amount_tva[$line->tva_tx] = 0.0;
325 $amount_ttc[$line->tva_tx] = 0.0;
326 $multicurrency_amount_ht[$line->tva_tx] = 0.0;
327 $multicurrency_amount_tva[$line->tva_tx] = 0.0;
328 $multicurrency_amount_ttc[$line->tva_tx] = 0.0;
330 $amount_ht[$line->tva_tx] += $line->total_ht;
331 $amount_tva[$line->tva_tx] += $line->total_tva;
332 $amount_ttc[$line->tva_tx] += $line->total_ttc;
333 $multicurrency_amount_ht[$line->tva_tx] += $line->multicurrency_total_ht;
334 $multicurrency_amount_tva[$line->tva_tx] += $line->multicurrency_total_tva;
335 $multicurrency_amount_ttc[$line->tva_tx] += $line->multicurrency_total_ttc;
340 foreach ($amount_ht as $tva_tx => $xxx) {
341 $discount->amount_ht = abs($amount_ht[$tva_tx]);
342 $discount->amount_tva = abs($amount_tva[$tva_tx]);
343 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
344 $discount->total_ht = abs($amount_ht[$tva_tx]);
345 $discount->total_tva = abs($amount_tva[$tva_tx]);
346 $discount->total_ttc = abs($amount_ttc[$tva_tx]);
347 $discount->multicurrency_amount_ht = abs($multicurrency_amount_ht[$tva_tx]);
348 $discount->multicurrency_amount_tva = abs($multicurrency_amount_tva[$tva_tx]);
349 $discount->multicurrency_amount_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
350 $discount->multicurrency_total_ht = abs($multicurrency_amount_ht[$tva_tx]);
351 $discount->multicurrency_total_tva = abs($multicurrency_amount_tva[$tva_tx]);
352 $discount->multicurrency_total_ttc = abs($multicurrency_amount_ttc[$tva_tx]);
353 $discount->tva_tx = abs((
float) $tva_tx);
355 $result = $discount->create($user);
371 $result = $invoice->setPaid($user,
'',
'');
373 $this->error = $invoice->error;
380 $hookmanager->initHooks(array(
'payment_supplierdao'));
381 $parameters = array(
'facid' => $facid,
'invoice' => $invoice,
'remaintopay' => $remaintopay);
382 $action =
'CLOSEPAIDSUPPLIERINVOICE';
383 $reshook = $hookmanager->executeHooks(
'createPayment', $parameters, $this, $action);
385 $this->error = $hookmanager->error;
387 } elseif ($reshook == 0) {
388 dol_syslog(
"Remain to pay for invoice " . $facid .
" not null. We do nothing more.");
396 $outputlangs = $langs;
398 $invoice->fetch_thirdparty();
399 $newlang = $invoice->thirdparty->default_lang;
401 if (!empty($newlang)) {
403 $outputlangs->setDefaultLang($newlang);
405 $ret = $invoice->fetch($facid);
406 $result = $invoice->generateDocument($invoice->model_pdf, $outputlangs);
413 $this->error = $this->db->lasterror();
417 dol_syslog(get_class($this).
'::Create Amount line '.$key.
' not a number. We discard it.');
423 $result = $this->call_trigger(
'PAYMENT_SUPPLIER_CREATE', $user);
430 $this->error = $this->db->lasterror();
434 $this->error =
"ErrorTotalIsNull";
435 dol_syslog(
'PaiementFourn::Create Error '.$this->error, LOG_ERR);
439 if ($totalamount != 0 && $error == 0) {
441 $this->total = $total;
442 $this->multicurrency_amount = $mtotal;
444 dol_syslog(
'PaiementFourn::Create Ok Total = '.$this->
amount.
', Total currency = '.$this->multicurrency_amount);
447 $this->db->rollback();