CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 [RESOLVED]Need help please- Update for FIFO inventory - next record problem

Print topic Send  topic

Author Message
doxtor

Posts: 24
Posted: 08/11/2008, 11:09 AM

Dear all..
I try to make an inventory control, with FIFO(first in first out) base, but I have a problem with the next record.
this is the code i make :
  
$db = new clsDBMassimo;  
	$product_id = $Stock->product_id->GetValue();  
	$AmOrdered = $Stock->stock_out_unit->GetValue();  
//check if the stock is enough  
	$sql = "SELECT SUM(stock_in_unit) as Total FROM stock_level WHERE product_id = $product_id";  
	$db->query($sql);  
	$result = $db->next_record();  
	if($result) ($Total = $db->f("Total"));  
//if stock not enough, then cancel insert and give error message, but i still have an error  
	if($Total < $AmOrdered){  
		$Stock->Errors->addError("Stock Not Enough");=>in this part also have problem.event   
		$db->close();    though I already add an error, but stock out still inserted on table.any suggestion?  
	}else{  //if the stock is enough then select from table stock_level  
		$sql = "SELECT * FROM stock_level WHERE product_id = $product_id ORDER BY in_date";  
		$db->query($sql);  
		$result= $db->next_record();  
		$IDnya = $db->f("stock_level_id");  
		$AmOnHand = $db->f("stock_in_unit");  
		  
		while($db->next_record()){  
//loop as long as amount ordered > 0  
			while ($AmOrdered > 0){  
				if($AmOrdered < $AmOnHand){  
					$Res = $AmOnHand - $AmOrdered;  
					$update = "UPDATE stock_level SET stock_in_unit = $Res where stock_level_id = $IDnya";  
					$db->query($update);  
					$AmOrdered = 0;  
				}else{  
					$AmOrdered = $AmOrdered - $AmOnHand;  
					$update = "UPDATE stock_level SET stock_in_unit = 0 where stock_level_id = $IDnya";  
					$db->query($update);  
//here should be move to the next record to get the next stock, but can not move to next record					  
				}  
			}  
		}  
		$db->close();  
	}    
Please give any suggestions, for the error I got or for the code structure. any helps are appreciated.
Thanks a lot
:-)
View profile  Send private message
doxtor

Posts: 24
Posted: 08/16/2008, 7:56 PM

Hi all...
I'm still stuck in this problem :(
please, need help.. :-/
View profile  Send private message
datadoit
Posted: 08/16/2008, 8:12 PM

I noticed you're using the same database connection in several updates
and loops. Try using different connections for different actions.

$db
$db1
$db2
etc.
wkempees


Posts: 1679
Posted: 08/17/2008, 6:38 AM

@Doxtor
Assume this code is in BeforeInsert
To check and stop insert:
  
  $product_id = $Stock->product_id->GetValue();  
  $AmOrdered  = $Stock->stock_out_unit->GetValue();  
  //check if the stock is enough  
  $InStock = CCDLookup('SUM(stock_in_unit)','stock_level','product_id='. $DBMassimo->ToSQL($product_id, ccsInteger),$DBMassimo);  
  //if stock not enough, then cancel insert and give error message, but i still have an error  
  if($InStock < $AmOrdered){  
    $Stock->InsertAllowed = False;   
    $Stock->Errors->addError("Stock Not Enough");  
    return;  
  }  

Rest will follow
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
wkempees


Posts: 1679
Posted: 08/17/2008, 7:19 AM

Total code: [edited 08/18/2008]

  
 $product_id = $Stock->product_id->GetValue();    
  $AmOrdered  = $Stock->stock_out_unit->GetValue();    
  //check if the stock is enough    
  $InStock = CCDLookup('SUM(stock_in_unit)','stock_level','product_id='. $DBMassimo->ToSQL($product_id, ccsInteger),$DBMassimo);    
  //if stock not enough, then cancel insert and give error message, but i still have an error    
  if($InStock < $AmOrdered){    
    $Stock->InsertAllowed = False;     
    $Stock->Errors->addError("Stock Not Enough");    
    return;    
  }    
// the RETURN above makes the BeforeInsert end here if error is posted.  
  
  $db = new clsDBMassimo;  // new connection  
  $dbU = new clsDBMassimo;  // new connection  
  $sql = "SELECT * FROM stock_level WHERE product_id =". $db->ToSQL($product_id, ccsInteger) ." ORDER BY in_date";  
  $db->query($sql);  
  while($db->next_record() ){  
    if($AmOrdered > 0){    // booked amounts are deducted when 0 we are finished.  
      $IDnya = $db->f("stock_level_id");  
      $AmOnHand = $db->f("stock_in_unit");  
      if($AmOrdered < $AmOnHand){  
        $Res = $AmOnHand - $AmOrdered;  
        $update = "UPDATE stock_level SET stock_in_unit =";  
        $update .= $dbU->ToSQL($Res, ccsInteger)           ;  
        $update .= " where stock_level_id ="              ;  
        $update .= $dbU->ToSQL($IDnya, ccsIteger)          ;  
        $dbU->query($update);  
        $AmOrdered = 0;  
      }else{ //$AmOrdered >= $AmOnHand  
        $update = "UPDATE stock_level SET stock_in_unit = 0";  
        $update .=" where stock_level_id ="                 ;  
        $update .=$dbU->ToSQL($IDnya, ccsInteger)            ;  
        $dbU->query($update);  
        $AmOrdered = $AmOrdered - $AmOnHand;  
      }  
    }  
  }  
  $db->close();  
  $dbU->close();  
  

Although I do not know your database schema, this is the best I can come up with.


Hope this helps you.

Walter

_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
datadoit
Posted: 08/17/2008, 9:04 AM

Isn't there a problem of using the same database connection and lookup
result with an inner loop database update or insert?

A database connection and query is performed with $db. In a Do/While
loop for that connection and resulting query, another database
Update/Insert is being performed using the same connection (and
resulting query).

I would think that would either throw an error or produce an unexpected
result.
doxtor

Posts: 24
Posted: 08/17/2008, 11:17 AM

thank you datadoit and walter. Give me a new hope :-)
I will try and report soon.

@walter: cancel insert can run perfectly. thank you.
Will report the rest
View profile  Send private message
doxtor

Posts: 24
Posted: 08/17/2008, 12:01 PM

Yesssss..... did it 8-)
First use code from walter to do the rest, but, datadoit right, use the same database connection give unexpected result. then i try to make 2 database connection, and it work. Yeeehaa... :-D

Thank you so muchh walter and datadoit. You're great :-)

Now do the rest, and wish all run perfectly :-)
View profile  Send private message
wkempees


Posts: 1679
Posted: 08/17/2008, 4:30 PM

http://donate.consultair.eu
And I will split with DD O:)

edited the code to use second (third) connection $dbU.

Glad you liked it.
DD, I would also have liked the whole update loop to be in AfterInsert,
if you catch my drift.

Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
datadoit
Posted: 08/17/2008, 6:04 PM

wkempees wrote:
> DD, I would also have liked the whole update loop to be in AfterInsert,
> if you catch my drift.
> -------------------------------------

Makes sense.

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.