Add amounts in stock

0

I need this occasion to add quantities in stock, for example, when registration is made of some item an input amount is added for example 2 , but later you want to update that same item adding 2 más , in total they should be 4 .

I tried different ways to do it but I have not succeeded, in my code at the beginning I try to save the current value before it is updated, then a update is made with the new quantity, then I get the new value , and I do the sum of the two quantities, to finish I do again a update sending the new quantity in the field quantityTicket

Doing the respective tests, does not add the quantity only changes it to the added value, that is if I have 4 and add 2, the new quantity is 2. which is completely wrong.

Could someone support me with this?

if($_SERVER['REQUEST_METHOD']=='POST'){
        if(isset($_POST['quantityTicket'])&& isset($_POST['price_Ticket'])  && isset($_POST['comentTicket']) && isset($_POST['unit_Ticket']) && isset($_POST['id_ticket']))
          {
            $connection = new MySqlServerConnection();
            date_default_timezone_set('America/Tijuana');
            $hoy = date("Y-m-d");

            $selectQty = "SELECT quantityTicket from inventory_ticket where id_ticket = ?";//selecciono el valor actual y lo guardo
            $getQty = $connection->executeNonQuery($selectQty,array($_POST['quantityTicket']));//se guarda el valor actual


              $query = "UPDATE inventory_ticket
                        SET quantityTicket = ?,price_Ticket=?,registerTicket=?,comentTicket=?,unit_Ticket=? WHERE id_ticket = ?";
                        //se actualiza quantityTickey con una cantidad nueva

              $result = $connection->executeNonQuery($query,array($_POST['quantityTicket'], $_POST['price_Ticket'], $hoy, $_POST['comentTicket'], $_POST['unit_Ticket'] , $_POST['id_ticket']));
              //postea la nueva cantidad

              $newQty = "SELECT quantityTicket from inventory_ticket where id_ticket = ?"; // obtengo la nueva cantidad
              $getNewQty = $connection->executeNonQuery($newQty,array($_POST['quantityTicket']));//la guardo

              $sumQty =intval($getQty[0][0] + intval($getNewQty[0][0])); //suma del valor actual a la cantidad nueva


              $allQty = 'UPDATE inventory_ticket set quantityTicket = quantityTicket + ?  where id_ticket = ?'; //actualizo el campo
              $resultQty = $connection->executeNonQuery($allQty,array($sumQty,$_POST['id_ticket'])); //posteo l anueva cantidad
              if ($result == 0)
              {
                echo json_encode(array(
                  'status' => '0',
                  'errorMessage' => 'entrada actualizado'
                ));
              }
    
asked by Pato 14.11.2018 в 18:52
source

1 answer

1

In the case of the Update, it should be:

$query = "UPDATE inventory_ticket
                        SET quantityTicket = quantityTicket + ?,price_Ticket=?,registerTicket=?,comentTicket=?,unit_Ticket=? WHERE id_ticket = ?";
                        //se actualiza quantityTickey con una cantidad nueva

or decrement:

$query = "UPDATE inventory_ticket
                        SET quantityTicket = quantityTicket - ?,price_Ticket=?,registerTicket=?,comentTicket=?,unit_Ticket=? WHERE id_ticket = ?";
                        //se actualiza quantityTickey con una cantidad nueva

I do not know if you understand me.

The other option, but not recommended, is that when you save, you get the current value with a select and then increase or decrease the value of the select and you do the update.

    
answered by 14.11.2018 / 19:00
source