Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop
This commit is contained in:
commit
36f7f32205
@ -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
|
||||
*
|
||||
|
||||
@ -719,6 +719,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
|
||||
{
|
||||
@ -2190,6 +2264,13 @@ else if ($id || $ref)
|
||||
print '<td>' . $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). '</td>';
|
||||
print '</tr>';
|
||||
}
|
||||
// add a 0 qty lot row to be able to add a lot
|
||||
print '<tr>';
|
||||
// Qty to ship or shipped
|
||||
print '<td>' . '<input name="qtyl'.$line_id.'_0" id="qtyl'.$line_id.'_0" type="text" size="4" value="0">' . '</td>';
|
||||
// Batch number managment
|
||||
print '<td>' . $formproduct->selectLotStock('', 'batchl'.$line_id.'_0', '', 1, 0, $lines[$i]->fk_product). '</td>';
|
||||
print '</tr>';
|
||||
}
|
||||
else if (! empty($conf->stock->enabled))
|
||||
{
|
||||
|
||||
@ -2543,9 +2543,9 @@ class ExpeditionLigne extends CommonObjectLine
|
||||
|
||||
if (! empty($batch) && $conf->productbatch->enabled)
|
||||
{
|
||||
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)) {
|
||||
dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id, batch=$batch");
|
||||
|
||||
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++;
|
||||
@ -2553,7 +2553,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++;
|
||||
@ -2580,7 +2580,7 @@ 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";
|
||||
@ -2592,22 +2592,23 @@ class ExpeditionLigne extends CommonObjectLine
|
||||
$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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user