/// <summary> /// If exist return existing pi /// </summary> /// <param name="PartCode"></param> /// <param name="PartType"></param> /// <param name="DealerCode"></param> /// <returns></returns> public static PartInfo AddPartToDealer(string PartCode, string PartType, string DealerCode) { //using (var db = new PartDataContext()) //{ var db = new PartDataContext(); // must create another data context var pi = db.PartInfos.SingleOrDefault(p => p.DealerCode == DealerCode && p.PartCode == PartCode && p.PartType == PartType); if (pi != null) return pi; // cancel check dealer, assume dealer is valid //Dealer dl = DealerDAO.GetDealerByCode(DealerCode); //if (dl == null) return null; pi = new PartInfo { DealerCode = DealerCode, PartCode = PartCode, PartType = PartType }; // cancel check part code, assume part code is valid //if (!PartDAO.IsPartCodeValid(PartCode, "P", DealerCode)) return null; db.PartInfos.InsertOnSubmit(pi); db.SubmitChanges(); // db.Dispose(); return pi; }
/// <summary> /// Inc Current Stock n Create "part safety" if needed /// </summary> /// <param name="pi"></param> /// <param name="Quantity"></param> /// <param name="warehouseId"></param> public static void IncCurrentStock(PartInfo pi, int Quantity, long warehouseId) { var ps = PartInfoDAO.GetPartSafety(pi.PartInfoId, warehouseId); if (ps == null) { var db = new PartDataContext(); // must create another data context ps = new PartSafety { WarehouseId = warehouseId, PartInfoId = pi.PartInfoId, CurrentStock = 0, SafetyQuantity = 0 }; db.PartSafeties.InsertOnSubmit(ps); db.SubmitChanges(); //db.Dispose(); } ps = PartInfoDAO.GetPartSafety(pi.PartInfoId, warehouseId); if (ps != null) { ps.CurrentStock += Quantity; if (ps.CurrentStock < 0) { throw new Exception(string.Format("On hand quantity of \"{0}\" will be negative!", pi.PartCode)); } } else throw new Exception(string.Format("Cannot create safatyStock to inc current stock for {0}!", pi.PartCode)); }