From 888c072af02ba8125777a4457362e5116abc8453 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 23 Feb 2021 14:48:54 +0100 Subject: [PATCH] NEW Can set a percentage when creating an invoice from another object --- htdocs/compta/facture/card.php | 158 +++++++++++++++++++++++++-------- htdocs/langs/en_US/bills.lang | 2 +- htdocs/langs/en_US/errors.lang | 2 + htdocs/langs/en_US/main.lang | 2 + 4 files changed, 124 insertions(+), 40 deletions(-) diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index 1d83bbd4b76..41178def47d 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -983,7 +983,7 @@ if (empty($reshook)) $action = 'create'; } - if (!($_POST['fac_replacement'] > 0)) { + if (!(GETPOST('fac_replacement', 'int') > 0)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ReplaceInvoice")), null, 'errors'); $action = 'create'; @@ -1298,17 +1298,21 @@ if (empty($reshook)) } } - // Standard or deposit or proforma invoice, not from a Predefined template invoice + // Standard or deposit invoice, not from a Predefined template invoice if ((GETPOST('type') == Facture::TYPE_STANDARD || GETPOST('type') == Facture::TYPE_DEPOSIT || GETPOST('type') == Facture::TYPE_PROFORMA || (GETPOST('type') == Facture::TYPE_SITUATION && !GETPOST('situations'))) && GETPOST('fac_rec') <= 0) { - if (GETPOST('socid', 'int') < 1) - { + $typeamount = GETPOST('typedeposit', 'aZ09'); + $valuestandardinvoice = price2num(str_replace('%', '', GETPOST('valuestandardinvoice', 'alpha')), 'MU'); + $valuedeposit = price2num(str_replace('%', '', GETPOST('valuedeposit', 'alpha')), 'MU'); + $dateinvoice = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int')); + $date_pointoftax = dol_mktime(12, 0, 0, GETPOST('date_pointoftaxmonth', 'int'), GETPOST('date_pointoftaxday', 'int'), GETPOST('date_pointoftaxyear', 'int')); + + if (GETPOST('socid', 'int') < 1) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Customer")), null, 'errors'); $action = 'create'; } - $dateinvoice = dol_mktime(12, 0, 0, GETPOST('remonth', 'int'), GETPOST('reday', 'int'), GETPOST('reyear', 'int')); if (empty($dateinvoice)) { $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors'); @@ -1319,7 +1323,33 @@ if (empty($reshook)) $action = 'create'; } - $date_pointoftax = dol_mktime(12, 0, 0, GETPOST('date_pointoftaxmonth', 'int'), GETPOST('date_pointoftaxday', 'int'), GETPOST('date_pointoftaxyear', 'int')); + + if (GETPOST('type') == Facture::TYPE_STANDARD) { + if ($valuestandardinvoice < 0 || $valuestandardinvoice > 100) { + setEventMessages($langs->trans("ErrorAPercentIsRequired"), null, 'errors'); + $error++; + $action = 'create'; + } + } elseif (GETPOST('type') == Facture::TYPE_DEPOSIT) { + if ($typeamount && !empty($origin) && !empty($originid)) + { + if ($typeamount == 'amount' && $valuedeposit <= 0) { + setEventMessages($langs->trans("ErrorAnAmountWithoutTaxIsRequired"), null, 'errors'); + $error++; + $action = 'create'; + } + if ($typeamount == 'variable' && $valuedeposit <= 0) { + setEventMessages($langs->trans("ErrorAPercentIsRequired"), null, 'errors'); + $error++; + $action = 'create'; + } + if ($typeamount == 'variablealllines' && $valuedeposit <= 0) { + setEventMessages($langs->trans("ErrorAPercentIsRequired"), null, 'errors'); + $error++; + $action = 'create'; + } + } + } if (!$error) { @@ -1345,8 +1375,7 @@ if (empty($reshook)) $object->multicurrency_code = GETPOST('multicurrency_code', 'alpha'); $object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int'); - if (GETPOST('type') == Facture::TYPE_SITUATION) - { + if (GETPOST('type') == Facture::TYPE_SITUATION) { $object->situation_counter = 1; $object->situation_final = 0; $object->situation_cycle_ref = $object->newCycle(); @@ -1361,8 +1390,8 @@ if (empty($reshook)) } $retained_warranty_date_limit = GETPOST('retained_warranty_date_limit'); - if (!empty($retained_warranty_date_limit) && $db->jdate($retained_warranty_date_limit)) { - $object->retained_warranty_date_limit = $db->jdate($retained_warranty_date_limit); + if (!empty($retained_warranty_date_limit) && dol_stringtotime($retained_warranty_date_limit)) { + $object->retained_warranty_date_limit = dol_stringtotime($retained_warranty_date_limit); } $object->retained_warranty_date_limit = !empty($object->retained_warranty_date_limit) ? $object->retained_warranty_date_limit : $object->calculate_date_lim_reglement($object->retained_warranty_fk_cond_reglement); @@ -1371,11 +1400,12 @@ if (empty($reshook)) // If creation from another object of another module (Example: origin=propal, originid=1) if (!empty($origin) && !empty($originid)) { + $regs = array(); // Parse element/subelement (ex: project_task) $element = $subelement = $origin; if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) { - $element = $regs [1]; - $subelement = $regs [2]; + $element = $regs[1]; + $subelement = $regs[2]; } // For compatibility @@ -1432,12 +1462,10 @@ if (empty($reshook)) dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines or deposit lines"); $result = $srcobject->fetch($object->origin_id); - $typeamount = GETPOST('typedeposit', 'aZ09'); - $valuedeposit = price2num(GETPOST('valuedeposit', 'alpha'), 'MU'); - - // If deposit invoice - if ($_POST['type'] == Facture::TYPE_DEPOSIT && in_array($typeamount, array('amount', 'variable'))) + // If deposit invoice - down payment with 1 line (fixed amount or percent) + if (GETPOST('type') == Facture::TYPE_DEPOSIT && in_array($typeamount, array('amount', 'variable'))) { + // Define the array $amountdeposit $amountdeposit = array(); if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA)) { @@ -1548,7 +1576,8 @@ if (empty($reshook)) } } - if ($_POST['type'] != Facture::TYPE_DEPOSIT || ($_POST['type'] == Facture::TYPE_DEPOSIT && $typeamount == 'variablealllines')) + // standard invoice, credit note, or down payment from a percent of all lines + if (GETPOST('type') != Facture::TYPE_DEPOSIT || (GETPOST('type') == Facture::TYPE_DEPOSIT && $typeamount == 'variablealllines')) { if ($result > 0) { @@ -1559,8 +1588,17 @@ if (empty($reshook)) $lines = $srcobject->lines; } - // If we create a deposit with all lines and a percent, we change amount - if ($_POST['type'] == Facture::TYPE_DEPOSIT && $typeamount == 'variablealllines') { + // If we create a standard invoice with a percent, we change amount by changing the qty + if (GETPOST('type') == Facture::TYPE_STANDARD && $valuestandardinvoice > 0 && $valuestandardinvoice < 100) { + if (is_array($lines)) { + foreach ($lines as $line) { + // We keep ->subprice and ->pa_ht, but we change the qty + $line->qty = price2num($line->qty * $valuestandardinvoice / 100, 'MS'); + } + } + } + // If we create a down payment with a percent on all lines, we change amount by changing the qty + if (GETPOST('type') == Facture::TYPE_DEPOSIT && $typeamount == 'variablealllines') { if (is_array($lines)) { foreach ($lines as $line) { // We keep ->subprice and ->pa_ht, but we change the qty @@ -2938,14 +2976,13 @@ if ($action == 'create') // Outstanding Bill $arrayoutstandingbills = $soc->getOutstandingBills(); $outstandingBills = $arrayoutstandingbills['opened']; - print ' ('.$langs->trans('CurrentOutstandingBill').': '; + print ' - '.$langs->trans('CurrentOutstandingBill').': '; print price($outstandingBills, '', $langs, 0, 0, -1, $conf->currency); if ($soc->outstanding_limit != '') { if ($outstandingBills > $soc->outstanding_limit) print img_warning($langs->trans("OutstandingBillReached")); print ' / '.price($soc->outstanding_limit, '', $langs, 0, 0, -1, $conf->currency); } - print ')'; print ''; print ''."\n"; } else { @@ -3049,7 +3086,6 @@ if ($action == 'create') } print ''.$langs->trans('Type').''; - print '
'."\n"; // Standard invoice @@ -3057,32 +3093,70 @@ if ($action == 'create') $tmp = ' '; $tmp = $tmp.''; $desc = $form->textwithpicto($tmp, $langs->transnoentities("InvoiceStandardDesc"), 1, 'help', '', 0, 3); + print ''; + print ''; + if ((($origin == 'propal') || ($origin == 'commande')) && (!empty($originid))) { + /*print '';*/ + print ''; + } + print '
'; print $desc; + print ''; + $arraylist = array( + //'amount' => $langs->transnoentitiesnoconv('FixAmount', $langs->transnoentitiesnoconv('Deposit')), + //'variable' => $langs->transnoentitiesnoconv('VarAmountOneLine', $langs->transnoentitiesnoconv('Deposit')), + 'variablealllines' => $langs->transnoentitiesnoconv('VarAmountAllLines') + ); + print $form->selectarray('typestandard', $arraylist, GETPOST('typestandard', 'aZ09'), 0, 0, 0, '', 1); + print ''; + print ''.$langs->trans('PercentOfOriginalObject').':'; + print '
'; print '
'; - if ((empty($origin)) || ((($origin == 'propal') || ($origin == 'commande')) && (!empty($originid)))) - { - // Deposit + if ((empty($origin)) || ((($origin == 'propal') || ($origin == 'commande')) && (!empty($originid)))) { + // Deposit - Down payment if (empty($conf->global->INVOICE_DISABLE_DEPOSIT)) { print '
'; $tmp = ' '; print ''; $tmp = $tmp.''; $desc = $form->textwithpicto($tmp, $langs->transnoentities("InvoiceDepositDesc"), 1, 'help', '', 0, 3); - print '
'; + print ''; + print ''; if (($origin == 'propal') || ($origin == 'commande')) { - print ''; - print ''; } - print '
'; print $desc; print ''; + print ''; $arraylist = array( 'amount' => $langs->transnoentitiesnoconv('FixAmount', $langs->transnoentitiesnoconv('Deposit')), 'variable' => $langs->transnoentitiesnoconv('VarAmountOneLine', $langs->transnoentitiesnoconv('Deposit')), @@ -3090,9 +3164,11 @@ if ($action == 'create') ); print $form->selectarray('typedeposit', $arraylist, GETPOST('typedeposit', 'aZ09'), 0, 0, 0, '', 1); print ''.$langs->trans('Value').':'; + print ''; + print ''.$langs->trans("AmountOrPercent").''; + print '
'; + print '
'; print '
'; } @@ -3570,6 +3646,8 @@ if ($action == 'create') // Lines from source (TODO Show them also when creating invoice from template invoice) if (!empty($origin) && !empty($originid) && is_object($objectsrc)) { + $langs->loadLangs(array('orders', 'propal')); + // TODO for compatibility if ($origin == 'contrat') { // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva @@ -3578,13 +3656,12 @@ if ($action == 'create') $objectsrc->update_price(1, - 1, 1); } - print "\n"; - print "\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''; - print ''; + print "\n\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''; + print ''; switch (get_class($objectsrc)) { case 'Propal': @@ -3606,13 +3683,16 @@ if ($action == 'create') $newclassname = get_class($objectsrc); } - print ''.$langs->trans($newclassname).''.$objectsrc->getNomUrl(1); + // Ref of origin + print ''.$langs->trans($newclassname).''; + print ''; + print $objectsrc->getNomUrl(1); // We check if Origin document (id and type is known) has already at least one invoice attached to it $objectsrc->fetchObjectLinked($originid, $origin, '', 'facture'); if (is_array($objectsrc->linkedObjects['facture']) && count($objectsrc->linkedObjects['facture']) >= 1) { setEventMessages('WarningBillExist', null, 'warnings'); - echo ' ('.$langs->trans('LatestRelatedBill').end($objectsrc->linkedObjects['facture'])->getNomUrl(1).')'; + echo ' - '.$langs->trans('LatestRelatedBill').' '.end($objectsrc->linkedObjects['facture'])->getNomUrl(1); } echo ''; print ''.$langs->trans('AmountHT').''.price($objectsrc->total_ht).''; diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 45246dcc412..1060c02ce96 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -413,7 +413,7 @@ PaymentCondition14DENDMONTH=Within 14 days following the end of the month FixAmount=Fixed amount - 1 line with label '%s' VarAmount=Variable amount (%% tot.) VarAmountOneLine=Variable amount (%% tot.) - 1 line with label '%s' -VarAmountAllLines=Variable amount (%% tot.) - all same lines +VarAmountAllLines=Variable amount (%% tot.) - all lines from origin # PaymentType PaymentTypeVIR=Bank transfer PaymentTypeShortVIR=Bank transfer diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index 545fcdb62fb..9e56c5f5c3a 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -258,6 +258,8 @@ ErrorLanguageRequiredIfPageIsTranslationOfAnother=The language of new page must ErrorLanguageMustNotBeSourceLanguageIfPageIsTranslationOfAnother=The language of new page must not be the source language if it is set as a translation of another page ErrorAParameterIsRequiredForThisOperation=A parameter is mandatory for this operation ErrorDateIsInFuture=Error, the date can't be in the future +ErrorAnAmountWithoutTaxIsRequired=Error, amount is mandatory +ErrorAPercentIsRequired=Error, please fill in the percentage correctly # Warnings WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Your PHP parameter upload_max_filesize (%s) is higher than PHP parameter post_max_size (%s). This is not a consistent setup. diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang index f7c55df211f..67544b46328 100644 --- a/htdocs/langs/en_US/main.lang +++ b/htdocs/langs/en_US/main.lang @@ -389,6 +389,8 @@ AmountTotal=Total amount AmountAverage=Average amount PriceQtyMinHT=Price quantity min. (excl. tax) PriceQtyMinHTCurrency=Price quantity min. (excl. tax) (currency) +PercentOfOriginalObject=Percent of original object +AmountOrPercent=Amount or percent Percentage=Percentage Total=Total SubTotal=Subtotal