diff --git a/htdocs/adherents/card_subscriptions.php b/htdocs/adherents/card_subscriptions.php index 491cbcf8b79..8e2eb19c19f 100644 --- a/htdocs/adherents/card_subscriptions.php +++ b/htdocs/adherents/card_subscriptions.php @@ -235,8 +235,8 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio { $db->begin(); + // Create subscription (and bank record if option is 'bankdirect') $crowid=$adh->cotisation($datecotisation, $cotisation, $accountid, $operation, $label, $num_chq, $emetteur_nom, $emetteur_banque, $datesubend, $option); - if ($crowid <= 0) { $error++; @@ -297,9 +297,9 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio // Validate invoice $result=$invoice->validate($user); + // Add payment if ($option == 'bankviainvoice') { - // Now we add payment require_once(DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'); require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'); @@ -324,7 +324,7 @@ if ($user->rights->adherent->cotisation->creer && $_POST["action"] == 'cotisatio if (! $error) { - $result=$paiement->addLinkInvoiceBank($user,'(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); + $result=$paiement->addPaymentToBank($user,'payment','(SubscriptionPayment)',$accountid,$emetteur_nom,$emetteur_banque); if (! $result > 0) { $errmsg=$paiement->error; diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php index 0fa93b6fe1e..2633b578ad1 100644 --- a/htdocs/adherents/class/adherent.class.php +++ b/htdocs/adherents/class/adherent.class.php @@ -1229,12 +1229,6 @@ class Adherent extends CommonObject } } - // Create invoice and payment - if ($option == 'bankviainvoice' && $accountid) - { - // TODO - } - // Change properties of object (used by triggers) $this->last_subscription_date=$dateop; $this->last_subscription_amount=$montant; diff --git a/htdocs/cashdesk/validation_verif.php b/htdocs/cashdesk/validation_verif.php index 80d623b11f9..03b294ca34b 100644 --- a/htdocs/cashdesk/validation_verif.php +++ b/htdocs/cashdesk/validation_verif.php @@ -244,69 +244,29 @@ switch ( $_GET['action'] ) $paiement_id = $payment->create($user); if ($paiement_id > 0) { - // Ajout d'une ecriture sur le compte bancaire - if ($conf->banque->enabled) - { - $bankaccountid=0; - if ( $obj_facturation->mode_reglement() == 'ESP' ) - { - $bankaccountid=$conf_fkaccount_cash; - } - if ( $obj_facturation->mode_reglement() == 'CHQ' ) - { - $bankaccountid=$conf_fkaccount_cheque; - } - if ( $obj_facturation->mode_reglement() == 'CB' ) - { - $bankaccountid=$conf_fkaccount_cb; - } + $bankaccountid=0; + if ( $obj_facturation->mode_reglement() == 'ESP' ) + { + $bankaccountid=$conf_fkaccount_cash; + } + if ( $obj_facturation->mode_reglement() == 'CHQ' ) + { + $bankaccountid=$conf_fkaccount_cheque; + } + if ( $obj_facturation->mode_reglement() == 'CB' ) + { + $bankaccountid=$conf_fkaccount_cb; + } - if ($bankaccountid > 0) - { - // Insertion dans llx_bank - $label = "(CustomerInvoicePayment)"; - $acc = new Account($db, $bankaccountid); - - $bank_line_id = $acc->addline($payment->datepaye, - $payment->paiementid, // Payment mode id or code ("CHQ or VIR for example") - $label, - $obj_facturation->prix_total_ttc(), - $payment->num_paiement, - '', - $user, - '', - ''); - - // Mise a jour fk_bank dans llx_paiement. - // On connait ainsi le paiement qui a genere l'ecriture bancaire - if ($bank_line_id > 0) - { - $payment->update_fk_bank($bank_line_id); - // Mise a jour liens (pour chaque facture concernees par le paiement) - foreach ($payment->amounts as $key => $value) - { - $facid = $key; - $fac = new Facture($db); - $fac->fetch($facid); - $fac->fetch_client(); - $acc->add_url_line($bank_line_id, - $paiement_id, - DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', - '(paiement)', - 'payment'); - $acc->add_url_line($bank_line_id, - $fac->client->id, - DOL_URL_ROOT.'/compta/fiche.php?socid=', - $fac->client->nom, - 'company'); - } - } - else - { - $error++; - } - } - } + if (! $error) + { + $result=$payment->addPaymentToBank($user,'payment','(CustomerInvoicePayment)',$bankaccountid,'',''); + if (! $result > 0) + { + $errmsg=$paiement->error; + $error++; + } + } } else { diff --git a/htdocs/compta/bank/class/account.class.php b/htdocs/compta/bank/class/account.class.php index 744836cf88a..2cee3af5905 100644 --- a/htdocs/compta/bank/class/account.class.php +++ b/htdocs/compta/bank/class/account.class.php @@ -202,6 +202,7 @@ class Account extends CommonObject // Clean parameters $emetteur=trim($emetteur); $banque=trim($banque); + if (is_numeric($oper)) // Clean oper to have a code instead of a rowid { $sql ="SELECT code FROM ".MAIN_DB_PREFIX."c_paiement"; @@ -236,7 +237,6 @@ class Account extends CommonObject return -3; } - $this->db->begin(); $datev = $date; diff --git a/htdocs/compta/paiement.php b/htdocs/compta/paiement.php index e7404dcce9a..022028257f2 100644 --- a/htdocs/compta/paiement.php +++ b/htdocs/compta/paiement.php @@ -48,6 +48,7 @@ $amountsresttopay=array(); $addwarning=0; // Security check +$socid=0; if ($user->societe_id > 0) { $socid = $user->societe_id; @@ -166,7 +167,7 @@ if ($_POST['action'] == 'confirm_paiement' && $_POST['confirm'] == 'yes') if (! $error) { - $result=$paiement->addLinkInvoiceBank($user,'(CustomerInvoicePayment)',$_POST['accountid'],$_POST['chqemetteur'],$_POST['chqbank']); + $result=$paiement->addPaymentToBank($user,'payment','(CustomerInvoicePayment)',$_POST['accountid'],$_POST['chqemetteur'],$_POST['chqbank']); if (! $result > 0) { $errmsg=$paiement->error; diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 320174213d9..fe2271a492d 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -304,13 +304,14 @@ class Paiement * A record into bank for payment with links between this bank record and invoices of payment. * All payment properties must have been set first like after a call to create(). * @param user Object of user making payment + * @param mode 'payment', 'payment_supplier' * @param label Label to use in bank record * @param accountid Id of bank account to do link with * @param emetteur_nom Name of transmitter * @param emetteur_banque Name of bank * @return int <0 if KO, >0 if OK */ - function addLinkInvoiceBank($user,$label,$accountid,$emetteur_nom,$emetteur_banque) + function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque) { global $conf; @@ -323,11 +324,15 @@ class Paiement $acc = new Account($this->db); $acc->fetch($accountid); + $total=$this->total; + if ($mode == 'payment_supplier') $total=-$total; + if ($mode == 'payment_sc') $total=-$total; + // Insert payment into llx_bank $bank_line_id = $acc->addline($this->datepaye, $this->paiementid, // Payment mode id or code ("CHQ or VIR for example") $label, - $this->total, + $total, $this->num_paiement, '', $user, @@ -344,33 +349,50 @@ class Paiement $error++; dol_print_error($this->db); } - // Add link 'payment' in bank_url between payment and bank transaction - $result=$acc->add_url_line($bank_line_id, - $this->id, - DOL_URL_ROOT.'/compta/paiement/fiche.php?id=', - '(paiement)', - 'payment'); - if ($result <= 0) + + // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction + $url=''; + if ($mode == 'payment') $url=DOL_URL_ROOT.'/compta/paiement/fiche.php?id='; + if ($mode == 'payment_supplier') $url=DOL_URL_ROOT.'/fourn/paiement/fiche.php?id='; + if ($url) { - $error++; - dol_print_error($this->db); + $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode); + if ($result <= 0) + { + $error++; + dol_print_error($this->db); + } } + // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment) $linkaddedforthirdparty=array(); foreach ($this->amounts as $key => $value) { - $fac = new Facture($this->db); - $fac->fetch($key); - $fac->fetch_thirdparty(); // This should be always same third party but we loop in case of. - if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty + if ($mode == 'payment') { - $result=$acc->add_url_line($bank_line_id, - $fac->client->id, - DOL_URL_ROOT.'/compta/fiche.php?socid=', - $fac->client->nom, - 'company'); - if ($result <= 0) dol_print_error($this->db); - $linkaddedforthirdparty[$fac->client->id]=$fac->client->id; // Mark as done for this thirdparty + $fac = new Facture($this->db); + $fac->fetch($key); + $fac->fetch_thirdparty(); // This should be always same third party but we loop in case of. + if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty + { + $result=$acc->add_url_line($bank_line_id, $fac->client->id, + DOL_URL_ROOT.'/compta/fiche.php?socid=', $fac->client->nom, 'company'); + if ($result <= 0) dol_print_error($this->db); + $linkaddedforthirdparty[$fac->client->id]=$fac->client->id; // Mark as done for this thirdparty + } + } + if ($mode == 'payment_supplier') + { + $fac = new FactureFournisseur($this->db); + $fac->fetch($key); + $fac->fetch_fournisseur(); // This should be always same third party but we loop in case of. + if (! in_array($fac->client->id,$linkaddedforthirdparty)) // Not yet done for this thirdparty + { + $result=$acc->add_url_line($bank_line_id, $fac->fournisseur->id, + DOL_URL_ROOT.'/fourn/fiche.php?socid=', $fac->fournisseur->nom, 'company'); + if ($result <= 0) dol_print_error($this->db); + $linkaddedforthirdparty[$fac->fournisseur->id]=$fac->fournisseur->id; // Mark as done for this thirdparty + } } } } diff --git a/htdocs/compta/paiement_charge.php b/htdocs/compta/paiement_charge.php index 0e028582fe6..6a7b4b161fb 100755 --- a/htdocs/compta/paiement_charge.php +++ b/htdocs/compta/paiement_charge.php @@ -30,11 +30,12 @@ include_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php"); $langs->load("bills"); $chid=isset($_GET["id"])?$_GET["id"]:$_POST["id"]; +$amounts = array(); -// Securite acces client +// Security check +$socid=0; if ($user->societe_id > 0) { - $action = ''; $socid = $user->societe_id; } @@ -74,14 +75,12 @@ if ($_POST["action"] == 'add_paiement') $paymentid = 0; // Read possible payments - $amounts = array(); foreach ($_POST as $key => $value) { if (substr($key,0,7) == 'amount_') { $other_chid = substr($key,7); - - $amounts[$other_chid] = $_POST[$key]; + $amounts[$other_chid] = price2num($_POST[$key]); } } @@ -95,57 +94,38 @@ if ($_POST["action"] == 'add_paiement') $paiement->paiementtype = $_POST["paiementtype"]; $paiement->num_paiement = $_POST["num_paiement"]; $paiement->note = $_POST["note"]; - $paymentid = $paiement->create($user); - $socialcontrib = new ChargeSociales($db); - $socialcontrib->fetch($paiement->chid); - - if ($paymentid > 0) + if (! $error) { - // On determine le montant total du paiement - $total=0; - foreach ($paiement->amounts as $key => $value) - { - $chid = $key; - $value = trim($value); - $amount = price2num(trim($value), 'MT'); // Un round est ok si nb avec '.' - $total += $amount; - } - - // Insertion dans llx_bank - $langs->load("banks"); - $label = $langs->transnoentities("SocialContributionPayment"); - $acc = new Account($db, $_POST["accountid"]); - $bank_line_id = $acc->addline($paiement->datepaye, $paiement->paiementtype, $label, -$total, $paiement->num_paiement, '', $user); - - // Mise a jour fk_bank dans llx_paiementcharge. On connait ainsi le paiement qui a genere l'ecriture bancaire - if ($bank_line_id > 0) - { - $paiement->update_fk_bank($bank_line_id); - - // Mise a jour liens (pour chaque charge concernee par le paiement) - foreach ($paiement->amounts as $key => $value) - { - $acc->add_url_line($bank_line_id, $chid, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc'); - $acc->add_url_line($bank_line_id, $paymentid, DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id=', '(paiement)','payment_sc'); - } - - $db->commit(); - - $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; - Header("Location: ".$loc); - exit; - } - else { - $db->rollback(); - $mesg = "Echec de la creation entree compte: ".$db->error(); - } - } - else - { - $db->rollback(); - $mesg = "Failed to create payment: paiement_id=".$paymentid." ".$db->error(); + $paymentid = $paiement->create($user); + if (! $paymentid > 0) + { + $errmsg=$paiement->error; + $error++; + } } + + if (! $error) + { + $result=$paiement->addPaymentToBank($user,'payment_sc','(SocialContributionPayment)',$_POST['accountid'],'',''); + if (! $result > 0) + { + $errmsg=$paiement->error; + $error++; + } + } + + if (! $error) + { + $db->commit(); + $loc = DOL_URL_ROOT.'/compta/sociales/charges.php?id='.$chid; + Header('Location: '.$loc); + exit; + } + else + { + $db->rollback(); + } } $_GET["action"]='create'; diff --git a/htdocs/compta/sociales/class/chargesociales.class.php b/htdocs/compta/sociales/class/chargesociales.class.php index 33101332829..942fe3582fb 100644 --- a/htdocs/compta/sociales/class/chargesociales.class.php +++ b/htdocs/compta/sociales/class/chargesociales.class.php @@ -374,7 +374,7 @@ class PaiementCharge extends CommonObject * \brief Constructor * \param DB Database handler */ - function Paiementcharge($DB) + function PaiementCharge($DB) { $this->db = $DB; return 1; @@ -414,7 +414,6 @@ class PaiementCharge extends CommonObject $total=0; foreach ($this->amounts as $key => $value) { - $facid = $key; $amount = price2num(trim($value), 'MT'); $total += $amount; } @@ -758,6 +757,100 @@ class PaiementCharge extends CommonObject } + /** + * A record into bank for payment with links between this bank record and invoices of payment. + * All payment properties must have been set first like after a call to create(). + * @param user Object of user making payment + * @param mode 'payment_sc' + * @param label Label to use in bank record + * @param accountid Id of bank account to do link with + * @param emetteur_nom Name of transmitter + * @param emetteur_banque Name of bank + * @return int <0 if KO, >0 if OK + */ + function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque) + { + global $conf; + + $error=0; + + if ($conf->banque->enabled) + { + require_once(DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'); + + $acc = new Account($this->db); + $acc->fetch($accountid); + + $total=$this->total; + if ($mode == 'payment_sc') $total=-$total; + + // Insert payment into llx_bank + $bank_line_id = $acc->addline($this->datepaye, + $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example") + $label, + $total, + $this->num_paiement, + '', + $user, + $emetteur_nom, + $emetteur_banque); + + // Mise a jour fk_bank dans llx_paiement. + // On connait ainsi le paiement qui a genere l'ecriture bancaire + if ($bank_line_id > 0) + { + $result=$this->update_fk_bank($bank_line_id); + if ($result <= 0) + { + $error++; + dol_print_error($this->db); + } + + // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction + $url=''; + if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/fiche.php?id='; + if ($url) + { + $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode); + if ($result <= 0) + { + $error++; + dol_print_error($this->db); + } + } + + // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment) + $linkaddedforthirdparty=array(); + foreach ($this->amounts as $key => $value) + { + if ($mode == 'payment_sc') + { + $socialcontrib = new ChargeSociales($this->db); + $socialcontrib->fetch($key); + $result=$acc->add_url_line($bank_line_id, $socialcontrib->id, + DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''),'sc'); + if ($result <= 0) dol_print_error($this->db); + } + } + } + else + { + $this->error=$acc->error; + $error++; + } + } + + if (! $error) + { + return 1; + } + else + { + return -1; + } + } + + /** * \brief Mise a jour du lien entre le paiement de charge et la ligne dans llx_bank generee * \param id_bank Id de la banque diff --git a/htdocs/fourn/facture/paiement.php b/htdocs/fourn/facture/paiement.php index 62595972c7d..95355101001 100644 --- a/htdocs/fourn/facture/paiement.php +++ b/htdocs/fourn/facture/paiement.php @@ -48,12 +48,13 @@ $offset = $limit * $page ; if (! $sortorder) $sortorder="DESC"; if (! $sortfield) $sortfield="p.rowid"; +$amounts = array(); + // Security check $socid=0; if ($user->societe_id > 0) { - $action = ''; - $socid = $user->societe_id; + $socid = $user->societe_id; } @@ -64,130 +65,96 @@ if ($user->societe_id > 0) */ if ($action == 'add_paiement') { - $error = 0; + $error = 0; - $datepaye = dol_mktime(12, 0 , 0, - $_POST['remonth'], - $_POST['reday'], - $_POST['reyear']); - $paiement_id = 0; - $total = 0; - // Genere tableau des montants amounts - $amounts = array(); - foreach ($_POST as $key => $value) - { - if (substr($key,0,7) == 'amount_') - { - $other_facid = substr($key,7); - $amounts[$other_facid] = $_POST[$key]; - $total = $total + $amounts[$other_facid]; - } - } + $datepaye = dol_mktime(12, 0 , 0, + $_POST['remonth'], + $_POST['reday'], + $_POST['reyear']); + $paiement_id = 0; + $total = 0; + // Genere tableau des montants amounts + foreach ($_POST as $key => $value) + { + if (substr($key,0,7) == 'amount_') + { + $other_facid = substr($key,7); + $amounts[$other_facid] = $_POST[$key]; + $total = $total + $amounts[$other_facid]; + } + } - // Effectue les verifications des parametres - if ($_POST['paiementid'] <= 0) - { - $mesg = '