@@ -166,40 +166,58 @@ def metadata(request):
166
166
167
167
168
168
@login_required
169
- def details (request ,pk ):
169
+ def details (request , pk ):
170
170
item = Stock .objects .get (id = pk )
171
+
171
172
if request .method == 'POST' :
172
173
newStockForm = NewStockForm (request .POST , instance = item )
173
174
dispenseForm = DispenseForm (request .POST , instance = item )
175
+
174
176
if newStockForm .is_valid ():
175
- history = StockHistory .objects .create (item_name = item .item_name ,
176
- stock_in = item .stock_in ,
177
- stock_out = item .stock_out ,
178
- stock_in_date = item .stock_in_date ,
179
- stock_out_date = item .stock_out_date )
177
+ # Save the original stock_out value before updating it to zero
178
+ original_stock_out = item .stock_out
179
+
180
+ # Set stock_out to zero
181
+ item .stock_out = 0
182
+
183
+ # Save the new stock entry
184
+ new_stock_instance = newStockForm .save ()
185
+
186
+
187
+ history = StockHistory .objects .create (
188
+ item_name = item .item_name ,
189
+ stock_in = new_stock_instance .stock_in ,
190
+ stock_out = original_stock_out , # Use the original stock_out value
191
+ stock_in_date = new_stock_instance .stock_in_date ,
192
+ stock_out_date = new_stock_instance .stock_out_date
193
+ )
180
194
history .save ()
181
- newStockForm . save ()
195
+
182
196
return redirect ('products' )
183
-
197
+
184
198
if dispenseForm .is_valid ():
185
- history = StockHistory .objects .create (item_name = item .item_name ,
186
- stock_in = item .stock_in ,
187
- stock_out = item .stock_out ,
188
- stock_in_date = item .stock_in_date ,
189
- stock_out_date = item .stock_out_date )
199
+ history = StockHistory .objects .create (
200
+ item_name = item .item_name ,
201
+ stock_in = item .stock_in ,
202
+ stock_out = item .stock_out ,
203
+ stock_in_date = item .stock_in_date ,
204
+ stock_out_date = item .stock_out_date
205
+ )
190
206
history .save ()
191
207
dispenseForm .save ()
192
208
return redirect ('products' )
193
-
209
+
194
210
else :
195
211
newStockForm = NewStockForm (instance = item )
196
212
dispenseForm = DispenseForm (instance = item )
213
+
197
214
context = {
198
- 'newStockForm' : newStockForm ,
199
- 'dispenseForm' : dispenseForm ,
200
- 'item' : item
215
+ 'newStockForm' : newStockForm ,
216
+ 'dispenseForm' : dispenseForm ,
217
+ 'item' : item
201
218
}
202
- return render (request ,'dashboard/details.html' ,context )
219
+
220
+ return render (request , 'dashboard/details.html' , context )
203
221
204
222
@login_required
205
223
def edit_inventory (request ,pk ):
0 commit comments