Merge pull request #5827 from aspangaro/5.0-p20
Work on supplier type (Replacement / deposit / credit note) invoice
This commit is contained in:
commit
468ff6795a
@ -355,11 +355,198 @@ if (empty($reshook))
|
|||||||
// Create
|
// Create
|
||||||
elseif ($action == 'add' && $user->rights->fournisseur->facture->creer)
|
elseif ($action == 'add' && $user->rights->fournisseur->facture->creer)
|
||||||
{
|
{
|
||||||
$error=0;
|
if ($socid > 0) $object->socid = GETPOST('socid', 'int');
|
||||||
|
|
||||||
|
$db->begin();
|
||||||
|
|
||||||
|
$error = 0;
|
||||||
|
|
||||||
|
// Fill array 'array_options' with data from add form
|
||||||
|
$extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
|
||||||
|
$ret = $extrafields->setOptionalsFromPost($extralabels, $object);
|
||||||
|
if ($ret < 0) $error++;
|
||||||
|
|
||||||
$datefacture=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
|
$datefacture=dol_mktime(12,0,0,$_POST['remonth'],$_POST['reday'],$_POST['reyear']);
|
||||||
$datedue=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
|
$datedue=dol_mktime(12,0,0,$_POST['echmonth'],$_POST['echday'],$_POST['echyear']);
|
||||||
|
|
||||||
|
// Replacement invoice
|
||||||
|
if ($_POST['type'] == FactureFournisseur::TYPE_REPLACEMENT)
|
||||||
|
{
|
||||||
|
if ($datefacture == '')
|
||||||
|
{
|
||||||
|
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('DateInvoice')), null, 'errors');
|
||||||
|
$action='create';
|
||||||
|
$_GET['socid']=$_POST['socid'];
|
||||||
|
$error++;
|
||||||
|
}
|
||||||
|
if (! ($_POST['fac_replacement'] > 0)) {
|
||||||
|
$error ++;
|
||||||
|
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("ReplaceInvoice")), null, 'errors');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $error) {
|
||||||
|
// This is a replacement invoice
|
||||||
|
$result = $object->fetch($_POST['fac_replacement']);
|
||||||
|
$object->fetch_thirdparty();
|
||||||
|
|
||||||
|
$object->ref = $_POST['ref'];
|
||||||
|
$object->ref_supplier = $_POST['ref_supplier'];
|
||||||
|
$object->socid = $_POST['socid'];
|
||||||
|
$object->libelle = $_POST['label'];
|
||||||
|
$object->date = $datefacture;
|
||||||
|
$object->date_echeance = $datedue;
|
||||||
|
$object->note_public = GETPOST('note_public');
|
||||||
|
$object->note_private = GETPOST('note_private');
|
||||||
|
$object->cond_reglement_id = GETPOST('cond_reglement_id');
|
||||||
|
$object->mode_reglement_id = GETPOST('mode_reglement_id');
|
||||||
|
$object->fk_account = GETPOST('fk_account', 'int');
|
||||||
|
$object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
|
||||||
|
$object->fk_incoterms = GETPOST('incoterm_id', 'int');
|
||||||
|
$object->location_incoterms = GETPOST('location_incoterms', 'alpha');
|
||||||
|
$object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
|
||||||
|
$object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
|
||||||
|
|
||||||
|
// Proprietes particulieres a facture de remplacement
|
||||||
|
$object->fk_facture_source = $_POST['fac_replacement'];
|
||||||
|
$object->type = FactureFournisseur::TYPE_REPLACEMENT;
|
||||||
|
|
||||||
|
$id = $object->createFromCurrent($user);
|
||||||
|
if ($id <= 0) {
|
||||||
|
setEventMessages($object->error, $object->errors, 'errors');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credit note invoice
|
||||||
|
if ($_POST['type'] == FactureFournisseur::TYPE_CREDIT_NOTE)
|
||||||
|
{
|
||||||
|
$sourceinvoice = GETPOST('fac_avoir');
|
||||||
|
if (! ($sourceinvoice > 0) && empty($conf->global->INVOICE_CREDIT_NOTE_STANDALONE))
|
||||||
|
{
|
||||||
|
$error ++;
|
||||||
|
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("CorrectInvoice")), null, 'errors');
|
||||||
|
}
|
||||||
|
if (GETPOST('socid','int')<1)
|
||||||
|
{
|
||||||
|
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('Supplier')), null, 'errors');
|
||||||
|
$action='create';
|
||||||
|
$error++;
|
||||||
|
}
|
||||||
|
if ($datefacture == '')
|
||||||
|
{
|
||||||
|
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('DateInvoice')), null, 'errors');
|
||||||
|
$action='create';
|
||||||
|
$_GET['socid']=$_POST['socid'];
|
||||||
|
$error++;
|
||||||
|
}
|
||||||
|
if (! GETPOST('ref_supplier'))
|
||||||
|
{
|
||||||
|
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('RefSupplier')), null, 'errors');
|
||||||
|
$action='create';
|
||||||
|
$_GET['socid']=$_POST['socid'];
|
||||||
|
$error++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $error)
|
||||||
|
{
|
||||||
|
$tmpproject = GETPOST('projectid', 'int');
|
||||||
|
|
||||||
|
// Creation facture
|
||||||
|
$object->ref = $_POST['ref'];
|
||||||
|
$object->ref_supplier = $_POST['ref_supplier'];
|
||||||
|
$object->socid = $_POST['socid'];
|
||||||
|
$object->libelle = $_POST['label'];
|
||||||
|
$object->date = $datefacture;
|
||||||
|
$object->date_echeance = $datedue;
|
||||||
|
$object->note_public = GETPOST('note_public');
|
||||||
|
$object->note_private = GETPOST('note_private');
|
||||||
|
$object->cond_reglement_id = GETPOST('cond_reglement_id');
|
||||||
|
$object->mode_reglement_id = GETPOST('mode_reglement_id');
|
||||||
|
$object->fk_account = GETPOST('fk_account', 'int');
|
||||||
|
$object->fk_project = ($tmpproject > 0) ? $tmpproject : null;
|
||||||
|
$object->fk_incoterms = GETPOST('incoterm_id', 'int');
|
||||||
|
$object->location_incoterms = GETPOST('location_incoterms', 'alpha');
|
||||||
|
$object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
|
||||||
|
$object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
|
||||||
|
|
||||||
|
// Proprietes particulieres a facture avoir
|
||||||
|
$object->fk_facture_source = $sourceinvoice > 0 ? $sourceinvoice : '';
|
||||||
|
$object->type = FactureFournisseur::TYPE_CREDIT_NOTE;
|
||||||
|
|
||||||
|
$id = $object->create($user);
|
||||||
|
|
||||||
|
if (GETPOST('invoiceAvoirWithLines', 'int')==1 && $id>0)
|
||||||
|
{
|
||||||
|
$facture_source = new FactureFournisseur($db); // fetch origin object
|
||||||
|
if ($facture_source->fetch($object->fk_facture_source)>0)
|
||||||
|
{
|
||||||
|
$fk_parent_line = 0;
|
||||||
|
|
||||||
|
foreach($facture_source->lines as $line)
|
||||||
|
{
|
||||||
|
// Reset fk_parent_line for no child products and special product
|
||||||
|
if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
|
||||||
|
$fk_parent_line = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$line->fk_facture = $object->id;
|
||||||
|
$line->fk_parent_line = $fk_parent_line;
|
||||||
|
|
||||||
|
$line->subprice =-$line->subprice; // invert price for object
|
||||||
|
$line->pa_ht = -$line->pa_ht;
|
||||||
|
$line->total_ht=-$line->total_ht;
|
||||||
|
$line->total_tva=-$line->total_tva;
|
||||||
|
$line->total_ttc=-$line->total_ttc;
|
||||||
|
$line->total_localtax1=-$line->total_localtax1;
|
||||||
|
$line->total_localtax2=-$line->total_localtax2;
|
||||||
|
|
||||||
|
$result = $line->insert();
|
||||||
|
|
||||||
|
$object->lines[] = $line; // insert new line in current object
|
||||||
|
|
||||||
|
// Defined the new fk_parent_line
|
||||||
|
if ($result > 0 && $line->product_type == 9) {
|
||||||
|
$fk_parent_line = $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$object->update_price(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(GETPOST('invoiceAvoirWithPaymentRestAmount', 'int')==1 && $id>0)
|
||||||
|
{
|
||||||
|
$facture_source = new FactureFournisseur($db); // fetch origin object if not previously defined
|
||||||
|
if ($facture_source->fetch($object->fk_facture_source)>0)
|
||||||
|
{
|
||||||
|
$totalpaye = $facture_source->getSommePaiement();
|
||||||
|
$totalcreditnotes = $facture_source->getSumCreditNotesUsed();
|
||||||
|
$totaldeposits = $facture_source->getSumDepositsUsed();
|
||||||
|
$remain_to_pay = abs($facture_source->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits);
|
||||||
|
|
||||||
|
$object->addline($langs->trans('invoiceAvoirLineWithPaymentRestAmount'),$remain_to_pay,1,0,0,0,0,0,'','','TTC');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add predefined lines
|
||||||
|
/*
|
||||||
|
TODO delete
|
||||||
|
for($i = 1; $i <= $NBLINES; $i ++) {
|
||||||
|
if ($_POST['idprod' . $i]) {
|
||||||
|
$product = new Product($db);
|
||||||
|
$product->fetch($_POST['idprod' . $i]);
|
||||||
|
$startday = dol_mktime(12, 0, 0, $_POST['date_start' . $i . 'month'], $_POST['date_start' . $i . 'day'], $_POST['date_start' . $i . 'year']);
|
||||||
|
$endday = dol_mktime(12, 0, 0, $_POST['date_end' . $i . 'month'], $_POST['date_end' . $i . 'day'], $_POST['date_end' . $i . 'year']);
|
||||||
|
$result = $object->addline($product->description, $product->price, $_POST['qty' . $i], $product->tva_tx, $product->localtax1_tx, $product->localtax2_tx, $_POST['idprod' . $i], $_POST['remise_percent' . $i], $startday, $endday, 0, 0, '', $product->price_base_type, $product->price_ttc, $product->type);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standard or deposit
|
||||||
|
if ($_POST['type'] == FactureFournisseur::TYPE_STANDARD || $_POST['type'] == FactureFournisseur::TYPE_DEPOSIT)
|
||||||
|
{
|
||||||
if (GETPOST('socid','int')<1)
|
if (GETPOST('socid','int')<1)
|
||||||
{
|
{
|
||||||
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('Supplier')), null, 'errors');
|
setEventMessages($langs->trans('ErrorFieldRequired',$langs->transnoentities('Supplier')), null, 'errors');
|
||||||
@ -382,16 +569,10 @@ if (empty($reshook))
|
|||||||
$error++;
|
$error++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fill array 'array_options' with data from add form
|
|
||||||
|
|
||||||
if (! $error)
|
if (! $error)
|
||||||
{
|
{
|
||||||
$db->begin();
|
$db->begin();
|
||||||
|
|
||||||
$extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
|
|
||||||
$ret = $extrafields->setOptionalsFromPost($extralabels, $object);
|
|
||||||
if ($ret < 0) $error++;
|
|
||||||
|
|
||||||
$tmpproject = GETPOST('projectid', 'int');
|
$tmpproject = GETPOST('projectid', 'int');
|
||||||
|
|
||||||
// Creation facture
|
// Creation facture
|
||||||
@ -593,6 +774,7 @@ if (empty($reshook))
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Edit line
|
// Edit line
|
||||||
elseif ($action == 'updateline' && $user->rights->fournisseur->facture->creer)
|
elseif ($action == 'updateline' && $user->rights->fournisseur->facture->creer)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user