/**
  * Create or update batch record (update table llx_product_batch)
  *
  * @param	array|int	$dluo	Could be either 
  *                              - int if id of product_batch
  *                              - or complete array('fk_product_stock'=>, 'eatby'=>, 'sellby'=> , 'batchnumber'=>)
  * @param	int			$qty	Quantity of product with batch number. May be a negative amount.
  * @return 	int   				<0 if KO, else return productbatch id
  */
 function _create_batch($dluo, $qty)
 {
     $pdluo = new Productbatch($this->db);
     $result = 0;
     // Try to find an existing record with same batch number or id
     if (is_numeric($dluo)) {
         $result = $pdluo->fetch($dluo);
         if (empty($pdluo->id)) {
             // We didn't find the line. May be it was deleted before by a previous move in same transaction.
             $this->error = 'Error. You ask a move on a record for a serial that does not exists anymore. May be you take the same serial on samewarehouse several times in same shipment or it was used by another shipment. Remove this shipment and prepare another one.';
             $this->errors[] = $this->error;
             $result = -2;
         }
     } else {
         if (is_array($dluo)) {
             if (isset($dluo['fk_product_stock'])) {
                 $vfk_product_stock = $dluo['fk_product_stock'];
                 $veatby = $dluo['eatby'];
                 $vsellby = $dluo['sellby'];
                 $vbatchnumber = $dluo['batchnumber'];
                 $result = $pdluo->find($vfk_product_stock, $veatby, $vsellby, $vbatchnumber);
             } else {
                 dol_syslog(get_class($this) . "::_create_batch array param dluo must contain at least key fk_product_stock" . $error, LOG_ERR);
                 $result = -1;
             }
         } else {
             dol_syslog(get_class($this) . "::_create_batch error invalid param dluo" . $error, LOG_ERR);
             $result = -1;
         }
     }
     if ($result >= 0) {
         // No error
         if ($pdluo->id > 0) {
             //print "Avant ".$pdluo->qty." Apres ".($pdluo->qty + $qty)."<br>";
             $pdluo->qty += $qty;
             if ($pdluo->qty == 0) {
                 $result = $pdluo->delete(0, 1);
             } else {
                 $result = $pdluo->update(0, 1);
             }
         } else {
             $pdluo->fk_product_stock = $vfk_product_stock;
             $pdluo->qty = $qty;
             $pdluo->eatby = $veatby;
             $pdluo->sellby = $vsellby;
             $pdluo->batch = $vbatchnumber;
             $result = $pdluo->create(0, 1);
             if ($result < 0) {
                 $this->error = $pdluo->error;
                 $this->errors = $pdluo->errors;
             }
         }
     }
     return $result;
 }
示例#2
0
}
// Update batch information
if ($action == 'updateline' && GETPOST('save') == $langs->trans('Save')) {
    $pdluo = new Productbatch($db);
    $result = $pdluo->fetch(GETPOST('pdluoid', 'int'));
    if ($result > 0) {
        if ($pdluo->id) {
            if (!GETPOST("sellby") && !GETPOST("eatby") && !GETPOST("batch_number")) {
                setEventMessage($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("atleast1batchfield")), 'errors');
            } else {
                $d_eatby = dol_mktime(12, 0, 0, $_POST['eatbymonth'], $_POST['eatbyday'], $_POST['eatbyyear']);
                $d_sellby = dol_mktime(12, 0, 0, $_POST['sellbymonth'], $_POST['sellbyday'], $_POST['sellbyyear']);
                $pdluo->batch = GETPOST("batch_number", 'san_alpha');
                $pdluo->eatby = $d_eatby;
                $pdluo->sellby = $d_sellby;
                $result = $pdluo->update($user);
                if ($result < 0) {
                    setEventMessages($pdluo->error, $pdluo->errors, 'errors');
                }
            }
        } else {
            setEventMessages($langs->trans('BatchInformationNotfound'), null, 'errors');
        }
    } else {
        setEventMessages($pdluo->error, null, 'errors');
    }
    header("Location: product.php?id=" . $id);
    exit;
}
/*
 * View
 /**
  * Create or update batch record
  *
  * @param	variant		$dluo	Could be either int if id of product_batch or array with at leat fk_product_stock
  * @param	int			$qty	Quantity of product with batch number
  * @return 	int   				<0 if KO, else return productbatch id
  */
 function _create_batch($dluo, $qty)
 {
     $pdluo = new Productbatch($this->db);
     //Try to find an existing record with batch same batch number or id
     if (is_numeric($dluo)) {
         $result = $pdluo->fetch($dluo);
     } else {
         if (is_array($dluo)) {
             if (isset($dluo['fk_product_stock'])) {
                 $vfk_product_stock = $dluo['fk_product_stock'];
                 $veatby = $dluo['eatby'];
                 $vsellby = $dluo['sellby'];
                 $vbatchnumber = $dluo['batchnumber'];
                 $result = $pdluo->find($vfk_product_stock, $veatby, $vsellby, $vbatchnumber);
             } else {
                 dol_syslog(get_class($this) . "::_create_batch array param dluo must contain at least key fk_product_stock" . $error, LOG_ERR);
                 $result = -1;
             }
         } else {
             dol_syslog(get_class($this) . "::_create_batch error invalid param dluo" . $error, LOG_ERR);
             $result = -1;
         }
     }
     //batch record found so we update it
     if ($result > 0) {
         if ($pdluo->id > 0) {
             $pdluo->qty += $qty;
             if ($pdluo->qty == 0) {
                 $result = $pdluo->delete(0, 1);
             } else {
                 $result = $pdluo->update(0, 1);
             }
         } else {
             $pdluo->fk_product_stock = $vfk_product_stock;
             $pdluo->qty = $qty;
             $pdluo->eatby = $veatby;
             $pdluo->sellby = $vsellby;
             $pdluo->batch = $vbatchnumber;
             $result = $pdluo->create(0, 1);
         }
         return $result;
     } else {
         return -1;
     }
 }