Skip to content

Commit

Permalink
NEW When down payment is entered, discount to reuse into final invoice
Browse files Browse the repository at this point in the history
is automatically created. This save one click into invoice workflow.
  • Loading branch information
eldy committed Apr 30, 2017
1 parent a9eeb82 commit 7c2b3b8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 33 deletions.
18 changes: 10 additions & 8 deletions htdocs/compta/facture/card.php
Expand Up @@ -611,7 +611,9 @@
{
$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)
{
Expand All @@ -630,20 +632,19 @@
$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
Expand All @@ -663,8 +664,9 @@
$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]);
Expand Down
38 changes: 19 additions & 19 deletions 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>
Expand Down Expand Up @@ -221,7 +221,7 @@
$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
{
Expand Down Expand Up @@ -249,15 +249,15 @@
// 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');
$paiement->note = GETPOST('comment');

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');
Expand All @@ -268,7 +268,7 @@
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)
{
Expand All @@ -281,7 +281,7 @@
{
$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)
{
Expand Down Expand Up @@ -309,7 +309,7 @@
* View
*/

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

$form=new Form($db);

Expand All @@ -324,8 +324,8 @@
$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)
Expand All @@ -347,7 +347,7 @@
}

// 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 () {
Expand All @@ -369,7 +369,7 @@ function setPaiementCode()
}
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);
}
}
Expand Down Expand Up @@ -437,14 +437,14 @@ function callForResult(imgId)
';

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";
}

Expand Down
60 changes: 55 additions & 5 deletions htdocs/compta/paiement/class/paiement.class.php
Expand Up @@ -255,7 +255,7 @@ function create($user,$closepaidinvoices=0)
{
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
}
}
Expand All @@ -275,11 +275,61 @@ function create($user,$closepaidinvoices=0)
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++;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion htdocs/langs/en_US/bills.lang
Expand Up @@ -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".
Expand Down

0 comments on commit 7c2b3b8

Please sign in to comment.