public async Task <IHttpActionResult> CreateNewCardIssuanceX(CardIssuanceModel entity) { string userId = User.Identity.GetUserId(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } #region Definations var job = await context.Jobs.FindAsync(entity.JobId); // ClientStockLog var issuanceStatusPartial = _repo.FindIssuanceStatusByName("Partial"); var issuanceStatusCompleted = _repo.FindIssuanceStatusByName("Completed"); var issuanceTypeNew = _repo.FindIssuanceTypeByName("New Issuance"); var issuanceJob = _repo.FindCardIssuanceByJobTrackerId(entity.JobTrackerId); //var jobTrackerJobId = _repo.FindJobTrackerByJobId(entity.JobId); var jobTracker = await context.JobTrackers.FindAsync(entity.JobTrackerId); var jobStatusPartial = _repo.FindJobStatusByName("Partial"); var jobStatusComplete = _repo.FindJobStatusByName("Completed"); var jobTrackerStatusCompleted = _repo.FindJobTrackerStatusByName("Completed"); var jobTrackerStatusPartial = _repo.FindJobTrackerStatusByName("Partial"); var jobTrackerStatusWIP = _repo.FindJobTrackerStatusByName("WIP"); // MIS Requirements var jobVariant = _repo.FindJobVariantByJobId(job.Id); var clientVaultReport = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); //var clientStockReport = _repo.FindClientStocktReportBySidProductId(jobVariant.SidProductId); var clientStockReportForTheDay = _repo.FindClientStocktReportForTheDay(jobVariant.SidProductId); // ClientStockReportFortheDay #endregion if (entity.TotalQuantity > job.Quantity) { return(BadRequest(ModelState)); } if (job != null) { if (issuanceJob == null) { // ClientVaultReport // ClientStockReport #region InitializedSetup if (clientVaultReport == null) { //create new var newClientValutReport = new ClientVaultReport() { SidProductId = jobVariant.SidProductId, OpeningStock = 0, ClosingStock = 0, }; var cvr = await CreateClientVaultReport(newClientValutReport); } if (clientStockReportForTheDay == null) { var clientVaultReport2 = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); //create new var newClientStockReport = new ClientStockReport() { SidProductId = jobVariant.SidProductId, ClientVaultReportId = clientVaultReport2.Id, FileName = job.JobName, QtyIssued = 0, WasteQty = 0, ReturnQty = 0, OpeningStock = 0, ClosingStock = 0, CreatedOn = DateTime.Now }; var csr = await CreateClientStockReport(newClientStockReport); } #endregion #region IssuanceRegion //Todo var clientVaultReport3 = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); // There is enough card in vault if (clientVaultReport3.OpeningStock > job.Quantity) { // Only Issue if ClientValutReport:OpeningStock > Quantity if (job.Quantity == entity.TotalQuantityIssued) { var clientStockReportForTheDay2 = _repo.FindClientStocktReportForTheDay(jobVariant.SidProductId); var clientVaultReport2 = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); #region JobProcessForCompleteIssuance // Update IsJobPartial Status Job updateJob = job; updateJob.IsJobPartial = false; var t0 = await UpdateJob(updateJob.Id, updateJob); //done // Update JobTracker JobTracker updateJobTracker = jobTracker; updateJobTracker.InventoryId = jobStatusComplete.Id; updateJobTracker.JobTrackerStatusId = jobTrackerStatusWIP.Id; var t1 = await UpdateJobTracker(updateJobTracker.Id, updateJobTracker); //done // Create CardIssuance var newCardIssuance = new CardIssuance() { JobTrackerId = entity.JobTrackerId, JobId = entity.JobId, IssuanceId = userId, //entity.IssuanceId, IssuanceStatusId = issuanceStatusCompleted.Id, CollectorId = entity.CollectorId, TotalQuantity = job.Quantity, TotalQuantityIssued = entity.TotalQuantityIssued, TotalQuantityRemain = 0 }; var t3 = await CreateCardIssuance(newCardIssuance); // Create CardIssuanceLog var lastIssuance = _repository.CardIssuances.Where(i => i.JobId == job.Id).OrderByDescending(x => x.Id).Take(1).ToList(); var newCardIssuanceLog = new CardIssuanceLog() { CardIssuanceId = lastIssuance[0].Id, IssuanceTypeId = issuanceTypeNew.Id, TotalQuantity = lastIssuance[0].TotalQuantity, QuantityIssued = lastIssuance[0].TotalQuantityIssued, QuantityRemain = lastIssuance[0].TotalQuantityRemain, IssuanceId = userId, CollectorId = entity.CollectorId, IssuedDate = DateTime.Now }; var t4 = await CreateCardIssuanceLog(newCardIssuanceLog); //Todo Where(i => i.CardIssuanceId == lastIssuance[0].Id) //// After all is done //// Create JobBatchTracker var lastIssuanceLog = _repository.CardIssuanceLogs.OrderByDescending(x => x.Id).Take(1).ToList(); var newJobBatchTracker = new JobBatchTracker() { JobId = job.Id, JobTrackerId = jobTracker.Id, CardIssuanceId = lastIssuance[0].Id, JobTrackerStatusId = jobTrackerStatusWIP.Id }; var t2 = await CreateJobBatchTracker(newJobBatchTracker); #endregion // Create ClientStockReport for the day, // Use it to create the ClientStockLog // Update ClientVaultReport by reducing it #region MISEntryTest // get ClientStockReport for the day, if null create var newClientStockLog = new ClientStockLog() { ClientStockReportId = clientStockReportForTheDay.Id, CardIssuanceId = issuanceJob.Id, IssuanceQty = entity.TotalQuantityIssued, OpeningStock = clientVaultReport.OpeningStock, ClosingStock = clientVaultReport.OpeningStock - entity.TotalQuantityIssued, }; var csl = await CreateClientStockLog(newClientStockLog); // Update ClientVaultReport ClientVaultReport updateClientVaultReport = clientVaultReport2; updateClientVaultReport.ClosingStock -= job.Quantity; // Update ClientStockReport ClientStockReport updateClientStockReport = clientStockReportForTheDay2; //updateClientStockReport.FileName = job.JobName; updateClientStockReport.QtyIssued = job.Quantity; updateClientStockReport.ClosingStock -= job.Quantity; #endregion #region MIS Report // get cientVaultReportId if (clientVaultReport == null) { #region clientVaultReportNull // create a new ClientVaultReport and // Continue with the Process //if (clientStockReport == null) //{ // #region clientStockReportNull // // Create get the last entry // // and continue // //// Create ClientStockLog // //var newClientStockLog = new ClientStockLog() // //{ // // lientStockReportId= 1, // // CardIssuanceId = 1, // // IssuanceQty = 1, // // OpeningStock = 1, // // ClosingStock = 1 // //}; // #endregion //} //else //{ // #region clientStockReport // //// Continue // //// Create ClientStockLog // //var newClientStockLog = new ClientStockLog() // //{ // //}; // #endregion //} #endregion } else { #region ClientVaultReport //// Continue with the Process //if (clientStockReport == null) //{ // #region clientStockReportNull // // Create get the last entry // // and continue // //// Create ClientStockLog // //var newClientStockLog = new ClientStockLog() // //{ // //}; // #endregion // // Create and continue //} //else //{ // // Continue // #region clientStockReport // //// Continue // //// Create ClientStockLog // //var newClientStockLog = new ClientStockLog() // //{ // //}; // #endregion //} #endregion } // Create ClientStockReport for the day, // Use it to create the ClientStockLog // Update ClientVaultReport // Create ClientStockLog(DailyReport) // { get ClientVaultReport, } // CardStock // Card Stock Log // ClientStock // ClientStockLog #endregion } else { var clientStockReportForTheDay2 = _repo.FindClientStocktReportForTheDay(jobVariant.SidProductId); var clientVaultReport2 = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); #region JobProcessForPartialIssuance // Update IsJobPartial Status Job updateJob = job; updateJob.IsJobPartial = true; //Marker var t0 = await UpdateJob(updateJob.Id, updateJob); // Update JobTracker JobTracker updateJobTracker = jobTracker; updateJobTracker.InventoryId = jobStatusComplete.Id; updateJobTracker.JobTrackerStatusId = jobTrackerStatusWIP.Id; var t1 = await UpdateJobTracker(updateJobTracker.Id, updateJobTracker); // Create CardIssuance entity.IssuanceStatusId = issuanceStatusPartial.Id; entity.TotalQuantityRemain = (job.Quantity - entity.TotalQuantityIssued); var newCardIssuance = new CardIssuance() { JobTrackerId = entity.JobTrackerId, JobId = entity.JobId, IssuanceId = userId, //entity.IssuanceId, IssuanceStatusId = issuanceStatusPartial.Id, CollectorId = entity.CollectorId, TotalQuantity = job.Quantity, TotalQuantityIssued = entity.TotalQuantityIssued, TotalQuantityRemain = entity.TotalQuantityRemain }; var t3 = await CreateCardIssuance(newCardIssuance); var lastIssuance = _repository.CardIssuances.Where(i => i.JobId == job.Id).OrderByDescending(x => x.Id).Take(1).ToList(); // Create CardIssuanceLog var newCardIssuanceLog = new CardIssuanceLog() { CardIssuanceId = lastIssuance[0].Id, IssuanceTypeId = issuanceTypeNew.Id, TotalQuantity = lastIssuance[0].TotalQuantity, QuantityIssued = lastIssuance[0].TotalQuantityIssued, QuantityRemain = lastIssuance[0].TotalQuantityRemain, IssuanceId = userId, CollectorId = entity.CollectorId, IssuedDate = DateTime.Now }; var t4 = await CreateCardIssuanceLog(newCardIssuanceLog); //Todo Where(i => i.CardIssuanceId == lastIssuance[0].Id) //// After all is done //// Create JobBatchTracker var lastIssuanceLog = _repository.CardIssuanceLogs.OrderByDescending(x => x.Id).Take(1).ToList(); var newJobBatchTracker = new JobBatchTracker() { JobId = job.Id, JobTrackerId = jobTracker.Id, CardIssuanceId = lastIssuance[0].Id, JobTrackerStatusId = jobTrackerStatusWIP.Id }; var t2 = await CreateJobBatchTracker(newJobBatchTracker); #endregion #region MISEntryTest // get ClientStockReport for the day, if null create var newClientStockLog = new ClientStockLog() { ClientStockReportId = clientStockReportForTheDay.Id, CardIssuanceId = issuanceJob.Id, IssuanceQty = entity.TotalQuantityIssued, OpeningStock = clientVaultReport.OpeningStock, ClosingStock = clientVaultReport.OpeningStock - entity.TotalQuantityIssued, }; var csl = await CreateClientStockLog(newClientStockLog); // Update ClientVaultReport ClientVaultReport updateClientVaultReport = clientVaultReport2; updateClientVaultReport.ClosingStock -= job.Quantity; // Update ClientStockReport ClientStockReport updateClientStockReport = clientStockReportForTheDay2; //updateClientStockReport.FileName = job.JobName; updateClientStockReport.QtyIssued = job.Quantity; updateClientStockReport.ClosingStock -= job.Quantity; #endregion } } #endregion } } return(Ok()); }
public async Task <IHttpActionResult> CreateNewCardIssuance(CardIssuanceModel entity) { string userId = User.Identity.GetUserId(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } #region Definations var job = await context.Jobs.FindAsync(entity.JobId); // ClientStockLog var issuanceStatusPartial = _repo.FindIssuanceStatusByName("Partial"); var issuanceStatusCompleted = _repo.FindIssuanceStatusByName("Completed"); var issuanceTypeNew = _repo.FindIssuanceTypeByName("New Issuance"); var issuanceJob = _repo.FindCardIssuanceByJobTrackerId(entity.JobTrackerId); //var jobTrackerJobId = _repo.FindJobTrackerByJobId(entity.JobId); var jobTracker = await context.JobTrackers.FindAsync(entity.JobTrackerId); var jobStatusPartial = _repo.FindJobStatusByName("Partial"); var jobStatusComplete = _repo.FindJobStatusByName("Completed"); var jobTrackerStatusCompleted = _repo.FindJobTrackerStatusByName("Completed"); var jobTrackerStatusPartial = _repo.FindJobTrackerStatusByName("Partial"); var jobTrackerStatusWIP = _repo.FindJobTrackerStatusByName("WIP"); // MIS Requirements var jobVariant = _repo.FindJobVariantByJobId(job.Id); var clientVaultReport = _repo.FindClientVaultReportBySidProductId(jobVariant.SidProductId); //var clientStockReport = _repo.FindClientStocktReportBySidProductId(jobVariant.SidProductId); var clientStockReportForTheDay = _repo.FindClientStocktReportForTheDay(jobVariant.SidProductId); // ClientStockReportFortheDay #endregion #region Validator if (entity.TotalQuantity > job.Quantity) { return(BadRequest(ModelState)); } // Todo if (clientVaultReport == null || clientVaultReport.OpeningStock < entity.TotalQuantityIssued) { return(BadRequest(ModelState)); } if (clientStockReportForTheDay == null) { //create new var newClientStockReport = new ClientStockReport() { SidProductId = jobVariant.SidProductId, ClientVaultReportId = clientVaultReport.Id, FileName = job.JobName, QtyIssued = 0, WasteQty = 0, ReturnQty = 0, OpeningStock = 0, ClosingStock = 0, CreatedOn = DateTime.Now }; var csr = await CreateClientStockReport(newClientStockReport); } #endregion #region IssuanceRegion if (job != null) { if (issuanceJob == null) { // get a new one var clientStockReportForTheDay2 = _repo.FindClientStocktReportForTheDay(jobVariant.SidProductId); // Issueance Section //================== if (job.Quantity == entity.TotalQuantityIssued) { // Complete Issuance #region GeneralIssuanceRegion // Create CardIssuance var newCardIssuance = new CardIssuance() { JobTrackerId = entity.JobTrackerId, JobId = entity.JobId, IssuanceId = userId, //entity.IssuanceId, IssuanceStatusId = issuanceStatusCompleted.Id, CollectorId = entity.CollectorId, TotalQuantity = job.Quantity, TotalQuantityIssued = entity.TotalQuantityIssued, TotalQuantityRemain = entity.TotalQuantityRemain //todo: should not be from client }; var t3 = await CreateCardIssuance(newCardIssuance); var lastIssuance = _repository.CardIssuances.Where(i => i.JobId == job.Id).OrderByDescending(x => x.Id).Take(1).ToList(); var newCardIssuanceLog = new CardIssuanceLog() { CardIssuanceId = lastIssuance[0].Id, IssuanceTypeId = issuanceTypeNew.Id, TotalQuantity = lastIssuance[0].TotalQuantity, QuantityIssued = lastIssuance[0].TotalQuantityIssued, QuantityRemain = lastIssuance[0].TotalQuantityRemain, IssuanceId = userId, CollectorId = entity.CollectorId, IssuedDate = DateTime.Now }; var t4 = await CreateCardIssuanceLog(newCardIssuanceLog); var lastIssuanceLog = _repository.CardIssuanceLogs.OrderByDescending(x => x.Id).Take(1).ToList(); var newJobBatchTracker = new JobBatchTracker() { JobId = job.Id, JobTrackerId = jobTracker.Id, CardIssuanceId = lastIssuance[0].Id, JobTrackerStatusId = jobTrackerStatusWIP.Id }; var t2 = await CreateJobBatchTracker(newJobBatchTracker); // Stock Log //============ // Check for today ClientStockReport // Update the Vault and StockReport ClientVaultReport updateVault = clientVaultReport; updateVault.OpeningStock = updateVault.ClosingStock; updateVault.ClosingStock -= entity.TotalQuantityIssued; var t0UpdateClientVault = await UpdateClientVaultReport(updateVault.Id, updateVault); var newClientStockLog = new ClientStockLog() { ClientStockReportId = clientStockReportForTheDay2.Id, CardIssuanceId = lastIssuance[0].Id, IssuanceQty = entity.TotalQuantityIssued, OpeningStock = updateVault.OpeningStock, ClosingStock = updateVault.OpeningStock - entity.TotalQuantityIssued }; var t2StockLog = await CreateClientStockLog(newClientStockLog); ClientStockReport updateDayStock = clientStockReportForTheDay2; updateDayStock.FileName = job.JobName; updateDayStock.QtyIssued = entity.TotalQuantityIssued; updateDayStock.OpeningStock = updateVault.OpeningStock; updateDayStock.ClosingStock = updateVault.OpeningStock - entity.TotalQuantityIssued; updateDayStock.CreatedOn = DateTime.Now; var t1ClientStock = await UpdateClientStockReport(updateDayStock.Id, updateDayStock); #endregion // Update IsJobPartial Status Job updateJob = job; updateJob.IsJobPartial = false; var tUpdateJob = await UpdateJob(updateJob.Id, updateJob); // Update JobTracker JobTracker updateJobTracker = jobTracker; updateJobTracker.InventoryId = jobStatusComplete.Id; updateJobTracker.JobTrackerStatusId = jobTrackerStatusWIP.Id; var tUpdateJobTracker = await UpdateJobTracker(updateJobTracker.Id, updateJobTracker); } else { // Partial Issuance #region GeneralIssuanceRegion // Create CardIssuance var newCardIssuance = new CardIssuance() { JobTrackerId = entity.JobTrackerId, JobId = entity.JobId, IssuanceId = userId, //entity.IssuanceId, IssuanceStatusId = issuanceStatusCompleted.Id, CollectorId = entity.CollectorId, TotalQuantity = job.Quantity, TotalQuantityIssued = entity.TotalQuantityIssued, TotalQuantityRemain = entity.TotalQuantityRemain //todo: should not be from client }; var t3 = await CreateCardIssuance(newCardIssuance); var lastIssuance = _repository.CardIssuances.Where(i => i.JobId == job.Id).OrderByDescending(x => x.Id).Take(1).ToList(); var newCardIssuanceLog = new CardIssuanceLog() { CardIssuanceId = lastIssuance[0].Id, IssuanceTypeId = issuanceTypeNew.Id, TotalQuantity = lastIssuance[0].TotalQuantity, QuantityIssued = lastIssuance[0].TotalQuantityIssued, QuantityRemain = lastIssuance[0].TotalQuantityRemain, IssuanceId = userId, CollectorId = entity.CollectorId, IssuedDate = DateTime.Now }; var t4 = await CreateCardIssuanceLog(newCardIssuanceLog); var lastIssuanceLog = _repository.CardIssuanceLogs.OrderByDescending(x => x.Id).Take(1).ToList(); var newJobBatchTracker = new JobBatchTracker() { JobId = job.Id, JobTrackerId = jobTracker.Id, CardIssuanceId = lastIssuance[0].Id, JobTrackerStatusId = jobTrackerStatusWIP.Id }; var t2 = await CreateJobBatchTracker(newJobBatchTracker); // Stock Log //============ // Check for today ClientStockReport // Update the Vault and StockReport ClientVaultReport updateVault = clientVaultReport; updateVault.OpeningStock = updateVault.ClosingStock; updateVault.ClosingStock -= entity.TotalQuantityIssued; var t0UpdateClientVault = await UpdateClientVaultReport(updateVault.Id, updateVault); var newClientStockLog = new ClientStockLog() { ClientStockReportId = clientStockReportForTheDay2.Id, CardIssuanceId = lastIssuance[0].Id, IssuanceQty = entity.TotalQuantityIssued, OpeningStock = updateVault.OpeningStock, ClosingStock = updateVault.OpeningStock - entity.TotalQuantityIssued }; var t2StockLog = await CreateClientStockLog(newClientStockLog); ClientStockReport updateDayStock = clientStockReportForTheDay2; updateDayStock.FileName = job.JobName; updateDayStock.QtyIssued = entity.TotalQuantityIssued; updateDayStock.OpeningStock = updateVault.OpeningStock; updateDayStock.ClosingStock = updateVault.OpeningStock - entity.TotalQuantityIssued; updateDayStock.CreatedOn = DateTime.Now; var t1ClientStock = await UpdateClientStockReport(updateDayStock.Id, updateDayStock); #endregion // Update IsJobPartial Status Job updateJob = job; updateJob.IsJobPartial = true; var t0 = await UpdateJob(updateJob.Id, updateJob); // Update JobTracker JobTracker updateJobTracker = jobTracker; updateJobTracker.InventoryId = jobStatusComplete.Id; updateJobTracker.JobTrackerStatusId = jobTrackerStatusWIP.Id; var t1 = await UpdateJobTracker(updateJobTracker.Id, updateJobTracker); // Create CardIssuance entity.IssuanceStatusId = issuanceStatusPartial.Id; entity.TotalQuantityRemain = (job.Quantity - entity.TotalQuantityIssued); } } } #endregion return(Ok()); }