diff --git a/htdocs/comm/propal/card.php b/htdocs/comm/propal/card.php index 83e905461c6..8dffa7ee5c3 100644 --- a/htdocs/comm/propal/card.php +++ b/htdocs/comm/propal/card.php @@ -690,10 +690,17 @@ if (empty($reshook)) { $deposit_percent_from_payment_terms = getDictvalue(MAIN_DB_PREFIX . 'c_payment_term', 'deposit_percent', $object->cond_reglement_id); if ( - !$error && GETPOST('statut', 'int') == $object::STATUS_SIGNED && GETPOST('generate_deposit', 'int') > 0 + !$error && GETPOST('statut', 'int') == $object::STATUS_SIGNED && GETPOST('generate_deposit', 'alpha') == 'on' && ! empty($deposit_percent_from_payment_terms) && ! empty($conf->facture->enabled) && ! empty($user->rights->facture->creer) ) { - $deposit = Facture::createDepositFromOrigin($object, $user, 0, GETPOST('validate_generated_deposit', 'int') > 0); + $date = dol_mktime(0, 0, 0, GETPOST('datefmonth', 'int'), GETPOST('datefday', 'int'), GETPOST('datefyear', 'int')); + $forceFields = array(); + + if (GETPOSTISSET('date_pointoftax')) { + $forceFields['date_pointoftax'] = dol_mktime(0, 0, 0, GETPOST('date_pointoftaxmonth', 'int'), GETPOST('date_pointoftaxday', 'int'), GETPOST('date_pointoftaxyear', 'int')); + } + + $deposit = Facture::createDepositFromOrigin($object, $date, GETPOST('cond_reglement_id', 'int'), $user, 0, GETPOST('validate_generated_deposit', 'alpha') == 'on', $forceFields); if ($deposit) { setEventMessage('DepositGenerated'); @@ -2005,33 +2012,89 @@ if ($action == 'create') { } } + if ($eligibleForDepositGeneration) { + $formquestion[] = array( + 'type' => 'checkbox', + 'tdclass' => 'showonlyifsigned', + 'name' => 'generate_deposit', + 'label' => $form->textwithpicto($langs->trans('GenerateDeposit', $object->deposit_percent), $langs->trans('PaymentConditionPermitsDepositGenerationSelected')) + ); + + $formquestion[] = array( + 'type' => 'date', + 'tdclass' => 'fieldrequired showonlyifgeneratedeposit', + 'name' => 'datef', + 'label' => $langs->trans('DateInvoice'), + 'value' => dol_now(), + 'datenow' => true + ); + + if (! empty($conf->global->INVOICE_POINTOFTAX_DATE)) { + $formquestion[] = array( + 'type' => 'date', + 'tdclass' => 'fieldrequired showonlyifgeneratedeposit', + 'name' => 'date_pointoftax', + 'label' => $langs->trans('DatePointOfTax'), + 'value' => dol_now(), + 'datenow' => true + ); + } + + ob_start(); + $form->select_conditions_paiements(0, 'cond_reglement_id', 1, 0, 0, 'minwidth200'); // TODO param 3 + $paymentTermsSelect = ob_get_clean(); + + $formquestion[] = array( + 'type' => 'other', + 'tdclass' => 'fieldrequired showonlyifgeneratedeposit', + 'name' => 'cond_reglement_id', + 'label' => $langs->trans('PaymentTerm'), + 'value' => $paymentTermsSelect + ); + + $formquestion[] = array( + 'type' => 'checkbox', + 'tdclass' => 'showonlyifgeneratedeposit', + 'name' => 'validate_generated_deposit', + 'label' => $langs->trans('ValidateGeneratedDeposit') + ); + $formquestion[] = array( 'type' => 'onecolumn', - 'name' => 'generate_deposit,validate_generated_deposit', 'value' => ' -
' diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index e209994c9ae..bf4271ef7d0 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -1411,9 +1411,10 @@ class Facture extends CommonInvoice * @param User $user * @param type $notrigger * @param bool $autoValidate + * @param array $overrideFields * @return Facture */ - static public function createDepositFromOrigin(CommonObject $origin, User $user, $notrigger = 0, $autoValidateDeposit = false, $forceInvoiceDate = null) + static public function createDepositFromOrigin(CommonObject $origin, $date, $cond_reglement_id, User $user, $notrigger = 0, $autoValidateDeposit = false, $overrideFields = array()) { global $conf, $langs, $hookmanager, $action; @@ -1422,16 +1423,19 @@ class Facture extends CommonInvoice return null; } + if (empty($date)) { + $origin->error = $langs->trans('ErrorFieldRequired', $langs->transnoentities('DateInvoice')); + return null; + } + require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php'; - $invoiceDate = ! empty($forceInvoiceDate) ? $forceInvoiceDate : dol_now(); - - if ($invoiceDate > (dol_get_last_hour(dol_now('tzuserrel')) + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) { + if ($date > (dol_get_last_hour(dol_now('tzuserrel')) + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) { $origin->error = 'ErrorDateIsInFuture'; return null; } - if ($origin->cond_reglement_id <= 0) { + if ($cond_reglement_id <= 0) { $origin->error = $langs->trans('ErrorFieldRequired', $langs->transnoentities('PaymentConditionsShort')); return null; } @@ -1453,9 +1457,9 @@ class Facture extends CommonInvoice $deposit->type = self::TYPE_DEPOSIT; $deposit->fk_project = $origin->fk_project; $deposit->ref_client = $origin->ref_client; - $deposit->date = $invoiceDate; - $deposit->mode_reglement_id = $origin->mode_reglement_id; // CHECK - // TODO Deposit is always due upon reception ? + $deposit->date = $date; + $deposit->mode_reglement_id = $origin->mode_reglement_id; + $deposit->cond_reglement_id = $cond_reglement_id; $deposit->availability_id = $origin->availability_id; $deposit->demand_reason_id = $origin->demand_reason_id; $deposit->fk_account = $origin->fk_account; @@ -1492,6 +1496,10 @@ class Facture extends CommonInvoice $deposit->linked_objects[$deposit->origin] = $deposit->origin_id; + foreach ($overrideFields as $key => $value) { + $deposit->$key = $value; + } + $deposit->context['createdepositfromorigin'] = 'createdepositfromorigin'; $origin->db->begin(); diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 61b00812efd..98ea68e5542 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -3877,7 +3877,7 @@ class Form * * @param int $selected Id of payment term to preselect by default * @param string $htmlname Nom de la zone select - * @param int $filtertype Not used + * @param int $filtertype If > 0, don't include payment terms with deposit percentage (for invoices) * @param int $addempty Add an empty entry * @param int $noinfoadmin 0=Add admin info, 1=Disable admin info * @param string $morecss Add more CSS on select tag @@ -3905,6 +3905,10 @@ class Form $selectedDepositPercent = null; foreach ($this->cache_conditions_paiements as $id => $arrayconditions) { + if ($filtertype > 0 && ! empty($arrayconditions['deposit_percent'])) { + continue; + } + if ($selected == $id) { $selectedDepositPercent = ! empty($deposit_percent) ? $deposit_percent : $arrayconditions['deposit_percent']; print '