From 28604b4d823ec8a92d4ba84654f198e98a8929c0 Mon Sep 17 00:00:00 2001 From: fappels Date: Thu, 2 Nov 2017 11:44:36 +0100 Subject: [PATCH 1/2] New add delete lot Add empty lot row for adding new lot When lot qty 0, lot will be deleted from line. --- htdocs/expedition/card.php | 81 ++++++++++++++++++++ htdocs/expedition/class/expedition.class.php | 36 ++++----- 2 files changed, 99 insertions(+), 18 deletions(-) diff --git a/htdocs/expedition/card.php b/htdocs/expedition/card.php index eb8c384e37e..ab30f083ebe 100644 --- a/htdocs/expedition/card.php +++ b/htdocs/expedition/card.php @@ -715,6 +715,80 @@ if (empty($reshook)) unset($_POST[$batch]); unset($_POST[$qty]); } + // add new batch + $lotStock = new Productbatch($db); + $batch="batchl".$line_id."_0"; + $qty = "qtyl".$line_id."_0"; + $batch_id = GETPOST($batch,'int'); + $batch_qty = GETPOST($qty, 'int'); + $lineIdToAddLot = 0; + if ($batch_qty > 0 && ! empty($batch_id)) + { + if ($lotStock->fetch($batch_id) > 0) + { + // check if lotStock warehouse id is same as line warehouse id + if ($lines[$i]->entrepot_id > 0) + { + // single warehouse shipment line + if ($lines[i]->entrepot_id == $lotStock->warehouseid) + { + $lineIdToAddLot = $line_id; + } + } + else if (count($lines[$i]->details_entrepot) > 1) + { + // multi warehouse shipment lines + foreach ($lines[$i]->details_entrepot as $detail_entrepot) + { + if ($detail_entrepot->entrepot_id == $lotStock->warehouseid) + { + $lineIdToAddLot = $detail_entrepot->line_id; + } + } + } + if ($lineIdToAddLot) + { + // add lot to existing line + if ($line->fetch($lineIdToAddLot) > 0) + { + $line->detail_batch->fk_origin_stock = $batch_id; + $line->detail_batch->batch = $lotStock->batch; + $line->detail_batch->entrepot_id = $lotStock->warehouseid; + $line->detail_batch->dluo_qty = $batch_qty; + if ($line->update($user) < 0) { + setEventMessages($line->error, $line->errors, 'errors'); + $error++; + } + } + else + { + setEventMessages($line->error, $line->errors, 'errors'); + $error++; + } + } + else + { + // create new line with new lot + $line->origin_line_id = $lines[$i]->origin_line_id; + $line->entrepot_id = $lotStock->warehouseid; + $line->detail_batch[0] = new ExpeditionLineBatch($db); + $line->detail_batch[0]->fk_origin_stock = $batch_id; + $line->detail_batch[0]->batch = $lotStock->batch; + $line->detail_batch[0]->entrepot_id = $lotStock->warehouseid; + $line->detail_batch[0]->dluo_qty = $batch_qty; + if ($object->create_line_batch($line, $line->array_options) < 0) + { + setEventMessages($object->error, $object->errors, 'errors'); + $error++; + } + } + } + else + { + setEventMessages($lotStock->error, $lotStock->errors, 'errors'); + $error++; + } + } } else { @@ -2180,6 +2254,13 @@ else if ($id || $ref) print '' . $formproduct->selectLotStock($detail_batch->fk_origin_stock, 'batchl'.$detail_batch->fk_expeditiondet.'_'.$detail_batch->fk_origin_stock, '', 1, 0, $lines[$i]->fk_product, $line->entrepot_id). ''; print ''; } + // add a 0 qty lot row to be able to add a lot + print ''; + // Qty to ship or shipped + print '' . '' . ''; + // Batch number managment + print '' . $formproduct->selectLotStock('', 'batchl'.$line_id.'_0', '', 1, 0, $lines[$i]->fk_product). ''; + print ''; } else if (! empty($conf->stock->enabled)) { diff --git a/htdocs/expedition/class/expedition.class.php b/htdocs/expedition/class/expedition.class.php index 3e341e6d5be..0e45248aab4 100644 --- a/htdocs/expedition/class/expedition.class.php +++ b/htdocs/expedition/class/expedition.class.php @@ -2547,7 +2547,7 @@ class ExpeditionLigne extends CommonObjectLine { dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch"); - if (empty($batch_id) || empty($expedition_batch_id) || empty($this->fk_product)) { + if (empty($batch_id) || empty($this->fk_product)) { dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR); $this->errors[]='ErrorMandatoryParametersNotProvided'; $error++; @@ -2555,7 +2555,7 @@ class ExpeditionLigne extends CommonObjectLine // fetch remaining lot qty require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionbatch.class.php'; - if (($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0) + if (! $error && ($lotArray = ExpeditionLineBatch::fetchAll($this->db, $this->id)) < 0) { $this->errors[]=$this->db->lasterror()." - ExpeditionLineBatch::fetchAll"; $error++; @@ -2582,34 +2582,34 @@ class ExpeditionLigne extends CommonObjectLine $this->errors[] = $lot->errors; $error++; } - else + if (! $error && ! empty($expedition_batch_id)) { // delete lot expedition line $sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch"; $sql.= " WHERE fk_expeditiondet = ".$this->id; $sql.= " AND rowid = ".$expedition_batch_id; - if (!$this->db->query($sql)) { $this->errors[]=$this->db->lasterror()." - sql=$sql"; $error++; } - else if ($qty > 0) + } + if (! $error && $this->detail_batch->dluo_qty > 0) + { + // create lot expedition line + if (isset($lot->id)) { - if (isset($lot->id)) + $shipmentLot = new ExpeditionLineBatch($this->db); + $shipmentLot->batch = $lot->batch; + $shipmentLot->eatby = $lot->eatby; + $shipmentLot->sellby = $lot->sellby; + $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id; + $shipmentLot->dluo_qty = $this->detail_batch->dluo_qty; + $shipmentLot->fk_origin_stock = $batch_id; + if ($shipmentLot->create($this->id) < 0) { - $shipmentLot = new ExpeditionLineBatch($this->db); - $shipmentLot->batch = $lot->batch; - $shipmentLot->eatby = $lot->eatby; - $shipmentLot->sellby = $lot->sellby; - $shipmentLot->entrepot_id = $this->detail_batch->entrepot_id; - $shipmentLot->dluo_qty = $this->detail_batch->dluo_qty; - $shipmentLot->fk_origin_stock = $batch_id; - if ($shipmentLot->create($this->id) < 0) - { - $this->errors[]=$shipmentLot->errors; - $error++; - } + $this->errors[]=$shipmentLot->errors; + $error++; } } } From 5febdf7bae523e3815a41ee4bcf1b1eac2f61e10 Mon Sep 17 00:00:00 2001 From: Neil Orley Date: Thu, 2 Nov 2017 15:01:38 +0100 Subject: [PATCH 2/2] NEW Add payment line to a specific invoice using the REST API Add payment line to a specific invoice using the REST API --- .../facture/class/api_invoices.class.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/htdocs/compta/facture/class/api_invoices.class.php b/htdocs/compta/facture/class/api_invoices.class.php index 801fb03e6fe..53a12c75674 100644 --- a/htdocs/compta/facture/class/api_invoices.class.php +++ b/htdocs/compta/facture/class/api_invoices.class.php @@ -779,6 +779,120 @@ class Invoices extends DolibarrApi return $result; } + /** + * Add payment line to a specific invoice + * + * The model schema is defined by the PaymentData class. + * + * @param int $id Id of invoice + * @param string $datepaye {@from body} Payment date {@type timestamp} + * @param int $paiementid {@from body} Payment mode Id {@min 1} + * @param string $closepaidinvoices {@from body} Close paid invoices {@choice yes,no} + * @param int $accountid {@from body} Account Id {@min 1} + * @param string $num_paiement {@from body} Payment number (optional) + * @param string $comment {@from body} Note (optional) + * @param string $chqemetteur {@from body} Payment issuer (mandatory if paiementcode = 'CHQ') + * @param string $chqbank {@from body} Issuer bank name (optional) + * + * @url POST {id}/addpayment + * + * @return int Payment ID + * @throws 400 + * @throws 401 + * @throws 404 + */ + function addPayment($id, $datepaye, $paiementid, $closepaidinvoices, $accountid, $num_paiement='', $comment='', $chqemetteur='', $chqbank='') { + global $conf; + + require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php'; + + if(! DolibarrApiAccess::$user->rights->facture->creer) { + throw new RestException(401); + } + if(empty($id)) { + throw new RestException(400, 'Invoice ID is mandatory'); + } + + if( ! DolibarrApi::_checkAccessToResource('facture',$id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $request_data = (object) $payment_data; + + if (! empty($conf->banque->enabled)) { + if(empty($accountid)) { + throw new RestException(400, 'Account ID is mandatory'); + } + } + + if(empty($paiementid)) { + throw new RestException(400, 'Paiement ID or Paiement Code is mandatory'); + } + + + $result = $this->invoice->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Invoice not found'); + } + + // Calculate amount to pay + $totalpaye = $this->invoice->getSommePaiement(); + $totalcreditnotes = $this->invoice->getSumCreditNotesUsed(); + $totaldeposits = $this->invoice->getSumDepositsUsed(); + $resteapayer = price2num($this->invoice->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT'); + + $this->db->begin(); + // Clean parameters amount if payment is for a credit note + if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) { + $resteapayer = price2num($resteapayer,'MT'); + $amounts[$id] = -$resteapayer; + // Multicurrency + $newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT'); + $multicurrency_amounts[$id] = -$newvalue; + } else { + $resteapayer = price2num($resteapayer,'MT'); + $amounts[$id] = $resteapayer; + // Multicurrency + $newvalue = price2num($this->invoice->multicurrency_total_ttc,'MT'); + $multicurrency_amounts[$id] = $newvalue; + } + + + // Creation of payment line + $paiement = new Paiement($this->db); + $paiement->datepaye = $datepaye; + $paiement->amounts = $amounts; // Array with all payments dispatching with invoice id + $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching + $paiement->paiementid = $paiementid; + $paiement->paiementcode = dol_getIdFromCode($this->db,$paiementid,'c_paiement','id','code',1); + $paiement->num_paiement = $num_paiement; + $paiement->note = $comment; + + $paiement_id = $paiement->create(DolibarrApiAccess::$user, ($closepaidinvoices=='yes'?1:0)); // This include closing invoices + if ($paiement_id < 0) + { + $this->db->rollback(); + throw new RestException(400, 'Payment error : '.$paiement->error); + } + + if (! empty($conf->banque->enabled)) { + $label='(CustomerInvoicePayment)'; + + if($paiement->paiementcode == 'CHQ' && empty($chqemetteur)) { + throw new RestException(400, 'Emetteur is mandatory when payment code is '.$paiement->paiementcode); + } + if ($this->invoice->type == Facture::TYPE_CREDIT_NOTE) $label='(CustomerInvoicePaymentBack)'; // Refund of a credit note + $result=$paiement->addPaymentToBank(DolibarrApiAccess::$user,'payment',$label,$accountid,$chqemetteur,$chqbank); + if ($result < 0) + { + $this->db->rollback(); + throw new RestException(400, 'Add payment to bank error : '.$paiement->error); + } + } + $this->db->commit(); + return $paiement_id; + } + /** * Clean sensible object datas *