From 7c2b3b8df0cb9b99456ac4010b1d0de913e6176f Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Sun, 30 Apr 2017 12:55:11 +0200 Subject: [PATCH] NEW When down payment is entered, discount to reuse into final invoice is automatically created. This save one click into invoice workflow. --- htdocs/compta/facture/card.php | 18 +++--- htdocs/compta/paiement.php | 38 ++++++------ .../compta/paiement/class/paiement.class.php | 60 +++++++++++++++++-- htdocs/langs/en_US/bills.lang | 2 +- 4 files changed, 85 insertions(+), 33 deletions(-) diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index 70eea532a79..6f5ef85310d 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -611,7 +611,9 @@ if (empty($reshook)) { $db->begin(); - // Boucle sur chaque taux de tva + $amount_ht = $amount_tva = $amount_ttc = array(); + + // Loop on each vat rate $i = 0; foreach ($object->lines as $line) { @@ -630,20 +632,19 @@ if (empty($reshook)) $discount->description = '(CREDIT_NOTE)'; elseif ($object->type == Facture::TYPE_DEPOSIT) $discount->description = '(DEPOSIT)'; - elseif ($object->type == Facture::TYPE_STANDARD) + elseif ($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT || $object->type == Facture::TYPE_SITUATION) $discount->description = '(EXCESS RECEIVED)'; else { setEventMessages($langs->trans('CantConvertToReducAnInvoiceOfThisType'), null, 'errors'); } - $discount->tva_tx = abs($object->total_ttc); $discount->fk_soc = $object->socid; $discount->fk_facture_source = $object->id; $error = 0; - if ($object->type == Facture::TYPE_STANDARD) { - - // If we're on a standard invoice, we have to get excess received to create it in TTC wuthout VAT + if ($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT || $object->type == Facture::TYPE_SITUATION) + { + // If we're on a standard invoice, we have to get excess received to create a discount in TTC without VAT $sql = 'SELECT SUM(pf.amount) as total_paiements FROM llx_c_paiement as c, llx_paiement_facture as pf, llx_paiement as p @@ -663,8 +664,9 @@ if (empty($reshook)) $error++; } - } else { - + } + if ($object->type == Facture::TYPE_CREDIT_NOTE || $object->type == Facture::TYPE_DEPOSIT) + { foreach ($amount_ht as $tva_tx => $xxx) { $discount->amount_ht = abs($amount_ht[$tva_tx]); diff --git a/htdocs/compta/paiement.php b/htdocs/compta/paiement.php index 96f77e85758..8cd924a0df8 100644 --- a/htdocs/compta/paiement.php +++ b/htdocs/compta/paiement.php @@ -1,6 +1,6 @@ <?php /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org> - * Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net> + * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2005 Marc Barilley / Ocebo <marc@ocebo.com> * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be> @@ -221,7 +221,7 @@ if (empty($reshook)) $db->begin(); // Clean parameters amount if payment is for a credit note - if (GETPOST('type') == 2) + if (GETPOST('type') == Facture::TYPE_CREDIT_NOTE) { foreach ($amounts as $key => $value) // How payment is dispatch { @@ -249,7 +249,7 @@ if (empty($reshook)) // Creation of payment line $paiement = new Paiement($db); $paiement->datepaye = $datepaye; - $paiement->amounts = $amounts; // Array with all payments dispatching + $paiement->amounts = $amounts; // Array with all payments dispatching with invoice id $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching $paiement->paiementid = dol_getIdFromCode($db,GETPOST('paiementcode'),'c_paiement'); $paiement->num_paiement = GETPOST('num_paiement'); @@ -257,7 +257,7 @@ if (empty($reshook)) if (! $error) { - $paiement_id = $paiement->create($user, (GETPOST('closepaidinvoices')=='on'?1:0)); + $paiement_id = $paiement->create($user, (GETPOST('closepaidinvoices')=='on'?1:0)); // This include closing invoices if ($paiement_id < 0) { setEventMessages($paiement->error, $paiement->errors, 'errors'); @@ -268,7 +268,7 @@ if (empty($reshook)) if (! $error) { $label='(CustomerInvoicePayment)'; - if (GETPOST('type') == 2) $label='(CustomerInvoicePaymentBack)'; + if (GETPOST('type') == Facture::TYPE_CREDIT_NOTE) $label='(CustomerInvoicePaymentBack)'; // Refund of a credit note $result=$paiement->addPaymentToBank($user,'payment',$label,GETPOST('accountid'),GETPOST('chqemetteur'),GETPOST('chqbank')); if ($result < 0) { @@ -281,7 +281,7 @@ if (empty($reshook)) { $db->commit(); - // If payment dispatching on more than one invoice, we keep on summary page, otherwise go on invoice card + // If payment dispatching on more than one invoice, we keep on summary page, otherwise jump on invoice card $invoiceid=0; foreach ($paiement->amounts as $key => $amount) { @@ -309,7 +309,7 @@ if (empty($reshook)) * View */ -llxHeader(); +llxHeader('', $langs->trans("Payment")); $form=new Form($db); @@ -324,8 +324,8 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie $facture->fetch_thirdparty(); $title=''; - if ($facture->type != 2) $title.=$langs->trans("EnterPaymentReceivedFromCustomer"); - if ($facture->type == 2) $title.=$langs->trans("EnterPaymentDueToCustomer"); + if ($facture->type != Facture::TYPE_CREDIT_NOTE) $title.=$langs->trans("EnterPaymentReceivedFromCustomer"); + if ($facture->type == Facture::TYPE_CREDIT_NOTE) $title.=$langs->trans("EnterPaymentDueToCustomer"); print load_fiche_titre($title); // Initialize data for confirmation (this is used because data can be change during confirmation) @@ -347,7 +347,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie } // Add realtime total information - if ($conf->use_javascript_ajax) + if (! empty($conf->use_javascript_ajax)) { print "\n".'<script type="text/javascript" language="javascript">'; print '$(document).ready(function () { @@ -369,7 +369,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie } if ($(\'#fieldchqemetteur\').val() == \'\') { - var emetteur = ('.$facture->type.' == 2) ? \''.dol_escape_js(dol_escape_htmltag($conf->global->MAIN_INFO_SOCIETE_NOM)).'\' : jQuery(\'#thirdpartylabel\').val(); + var emetteur = ('.$facture->type.' == '.Facture::TYPE_CREDIT_NOTE.') ? \''.dol_escape_js(dol_escape_htmltag($conf->global->MAIN_INFO_SOCIETE_NOM)).'\' : jQuery(\'#thirdpartylabel\').val(); $(\'#fieldchqemetteur\').val(emetteur); } } @@ -437,14 +437,14 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie '; print ' });'."\n"; - if (!empty($conf->use_javascript_ajax)){ - //Add js for AutoFill - print ' $(document).ready(function () {'; - print ' $(".AutoFillAmout").on(\'click touchstart\', function(){ - $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change"); - });'; - print ' });'."\n"; - } + + //Add js for AutoFill + print ' $(document).ready(function () {'; + print ' $(".AutoFillAmout").on(\'click touchstart\', function(){ + $("input[name="+$(this).data(\'rowname\')+"]").val($(this).data("value")).trigger("change"); + });'; + print ' });'."\n"; + print ' </script>'."\n"; } diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 53035dca96f..03a7759e52c 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -255,7 +255,7 @@ class Paiement extends CommonObject { if (! empty($conf->prelevement->enabled)) { - // TODO Check if this payment has a withdraw request + // FIXME Check if this invoice has a withdraw request // if not, $mustwait++; // This will disable automatic close on invoice to allow to process } } @@ -275,11 +275,61 @@ class Paiement extends CommonObject else if ($mustwait) dol_syslog("There is ".$mustwait." differed payment to process, we do nothing more."); else { - $result=$invoice->set_paid($user,'',''); - if ($result<0) + // If invoice is a down payment, we also convert down payment to discount + if ($invoice->type == Facture::TYPE_DEPOSIT) { - $this->error=$invoice->error; - $error++; + $amount_ht = $amount_tva = $amount_ttc = array(); + + // Loop on each vat rate + $i = 0; + foreach ($invoice->lines as $line) + { + if ($line->total_ht!=0) + { // no need to create discount if amount is null + $amount_ht[$line->tva_tx] += $line->total_ht; + $amount_tva[$line->tva_tx] += $line->total_tva; + $amount_ttc[$line->tva_tx] += $line->total_ttc; + $i ++; + } + } + + // Insert one discount by VAT rate category + $discount = new DiscountAbsolute($this->db); + $discount->description = '(DEPOSIT)'; + $discount->fk_soc = $invoice->socid; + $discount->fk_facture_source = $invoice->id; + + foreach ($amount_ht as $tva_tx => $xxx) + { + $discount->amount_ht = abs($amount_ht[$tva_tx]); + $discount->amount_tva = abs($amount_tva[$tva_tx]); + $discount->amount_ttc = abs($amount_ttc[$tva_tx]); + $discount->tva_tx = abs($tva_tx); + + $result = $discount->create($user); + if ($result < 0) + { + $error++; + break; + } + } + + if ($error) + { + setEventMessages($discount->error, $discount->errors, 'errors'); + $error++; + } + } + + // Set invoice to paid + if (! $error) + { + $result=$invoice->set_paid($user,'',''); + if ($result<0) + { + $this->error=$invoice->error; + $error++; + } } } } diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 7432f0e2412..189ef5c1fe2 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -447,7 +447,7 @@ CantRemovePaymentWithOneInvoicePaid=Can't remove payment since there is at least ExpectedToPay=Expected payment CantRemoveConciliatedPayment=Can't remove conciliated payment PayedByThisPayment=Paid by this payment -ClosePaidInvoicesAutomatically=Classify "Paid" all standard, situation or replacement invoices entirely paid. +ClosePaidInvoicesAutomatically=Classify "Paid" all standard, down payment or replacement invoices entirely paid. ClosePaidCreditNotesAutomatically=Classify "Paid" all credit notes entirely paid back. ClosePaidContributionsAutomatically=Classify "Paid" all social or fiscal contributions entirely paid. AllCompletelyPayedInvoiceWillBeClosed=All invoice with no remain to pay will be automatically closed to status "Paid". -- GitLab