Fix: Reset stock was not working because:

1. The frontend did not set the date format and that mangled the date string sent
 2. Backend was broken and was not acceping data in json format

Fix: Ledger would occasionally crap out due to rounding and floating point madness, removed the rounding limitation
This commit is contained in:
2020-09-20 09:56:35 +05:30
parent cb66700157
commit 005d45032f
6 changed files with 70 additions and 40 deletions

View File

@ -1,6 +1,4 @@
import uuid
from datetime import datetime
from decimal import Decimal
from fastapi import (
APIRouter,
@ -15,6 +13,7 @@ from ..models.master import Product, CostCentre, AccountBase
from ..schemas.auth import UserToken
from ..core.security import get_current_active_user as get_user
from ..db.session import SessionLocal
from ..schemas.settings import ResetStock
router = APIRouter()
@ -31,22 +30,16 @@ def get_db() -> Session:
@router.post("/{id_}")
def rebase(
id_: uuid.UUID,
quantity: Decimal,
stockDate: str,
resetDate: str,
item: ResetStock,
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["reset-stock"]),
):
product: Product = db.query(Product).filter(Product.id == id_).first()
quantity = round(quantity, 2)
stock_date = datetime.strptime(stockDate, "%d-%b-%Y").date()
reset_date = datetime.strptime(resetDate, "%d-%b-%Y").date()
if reset_date > stock_date:
if item.reset_date > item.stock_date:
raise ValueError("Reset cannot be after the stock date")
change = quantity - get_closing_stock(product, stock_date, db=db)
change = round(item.quantity, 2) - get_closing_stock(product, item.stock_date, db=db)
if change == 0:
return {"No Change Needed"}
final = get_closing_stock(product, db=db)
@ -56,7 +49,7 @@ def rebase(
batch = get_last_batch(product, db)
set_batches(batch, final + change, db)
create_voucher(batch, change, reset_date, user.id_, db)
create_voucher(batch, change, item.reset_date, user.id_, db)
db.commit()
return {}

View File

@ -15,8 +15,8 @@ class LedgerItem(BaseModel):
url: List[str]
type_: str
narration: str
debit: Decimal = Field(multiple_of=0.01)
credit: Decimal = Field(multiple_of=0.01)
debit: Decimal
credit: Decimal
posted: bool
@validator("date_", pre=True)

View File

@ -1,5 +1,5 @@
import uuid
from typing import List, Optional
from decimal import Decimal
from typing import Optional
from datetime import datetime, date
from pydantic import BaseModel, validator
@ -38,3 +38,27 @@ class LockInformation(BaseModel):
class Maintenance(BaseModel):
enabled: bool
user: Optional[str]
class ResetStock(BaseModel):
quantity: Decimal
stock_date: date
reset_date: date
class Config:
alias_generator = to_camel
json_encoders = {
date: lambda v: v.strftime("%d-%b-%Y"),
}
@validator("stock_date", pre=True)
def parse_stock_date(cls, value):
if isinstance(value, date):
return value
return datetime.strptime(value, "%d-%b-%Y").date()
@validator("reset_date", pre=True)
def parse_reset_date(cls, value):
if isinstance(value, date):
return value
return datetime.strptime(value, "%d-%b-%Y").date()