Frage

I have stock table like below:

date         item   new stock    sale  total
1-1-2000     abc      3          2     1
1-1-2000     bcd      4          2     2
1-1-2000     ffs      9          1     8

I need to that 'total value should be carry over next day for each products. how do I do that in mySQL. The data carry over process is on each day. If I add entry on next day, it should calculate last day value of 'total'.

War es hilfreich?

Lösung

CREATE TABLE items (
itemid int not null AUTO_INCREMENT PRIMARY KEY,
itemname varchar(40) not null,
onhand int not null, # current inv based on sales and inv adjustments
price decimal (10,2) not null   # default price per item
) engine=innodb;

CREATE TABLE sale (
saleid int not null AUTO_INCREMENT PRIMARY KEY, # basically an invoice #
customerid int not null,    # joined to a customer table
saledate timestamp not null
) engine=innodb;

CREATE TABLE sale_detail (
saleid int not null,        # invoice #
lineid int not null,        # lines 1 and up for this invoice
itemid int not null,        # item sold
qty int not null,           # quantity sold
price decimal (10,2) not null   # price per item can be overriden from items table value
) engine=innodb;

CREATE TABLE inventory_adj (
adjid int not null AUTO_INCREMENT PRIMARY KEY,
itemid int not null,
trans_typ int not null,     # 0=purchase stock, 1=inventory adj, 2=shrinkage, 3=return, etc
adjdate timestamp not null, # now()
qty int not null            # amt of change, positive is acquisition, negative is depletion
) engine=innodb;

prime your items table with onhand amount. this is your inventory level. goes down from sales, goes up with purchases into the inventory_adj table, gets adj there too when you take inventory, etc. this is a normalized data model. True, the onhand in items does not technically need to be kept, it can be calculated on the fly but that is a little slow. As long as you create transactions and update onhand based on sales etc and commit the transaction you are safe. Plus this gives you a better audit on what happened to get you there. Of course add other columns like the name of the user making the entries in sales and inventory adj.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top