public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            int Count = 0;

            if (this.Unit == null || string.IsNullOrWhiteSpace(this.Unit._id))
            {
                yield return(new ValidationResult("Unit is required", new List <string> {
                    "Unit"
                }));
            }

            if (string.IsNullOrWhiteSpace(this.Type))
            {
                yield return(new ValidationResult("Type is required", new List <string> {
                    "Type"
                }));
            }

            if (MaterialDistributionNoteItems.Count.Equals(0))
            {
                yield return(new ValidationResult("Material Distribution Note Item is required", new List <string> {
                    "MaterialDistributionNoteItemCollection"
                }));
            }
            else
            {
                string materialDistributionNoteItemError = "[";

                foreach (MaterialDistributionNoteItemViewModel mdni in this.MaterialDistributionNoteItems)
                {
                    if (string.IsNullOrWhiteSpace(mdni.MaterialRequestNoteCode))
                    {
                        Count++;
                        materialDistributionNoteItemError += "{ MaterialRequestNote: 'SPB is required' }, ";
                    }
                }

                if (Count.Equals(0))
                {
                    /* Get Inventory Summaries */
                    string inventorySummaryURI = "inventory/inventory-summary?order=%7B%7D&page=1&size=1000000000&";

                    MaterialDistributionNoteService Service = (MaterialDistributionNoteService)validationContext.GetService(typeof(MaterialDistributionNoteService));
                    HttpClient httpClient = new HttpClient();
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Service.Token);

                    List <string> products = new List <string>();
                    foreach (MaterialDistributionNoteItemViewModel mdni in this.MaterialDistributionNoteItems)
                    {
                        products.AddRange(mdni.MaterialDistributionNoteDetails.Select(p => p.Product.code).ToList());
                    }

                    var storageName = this.Unit.name.Equals("PRINTING") ? "Gudang Greige Printing" : "Gudang Greige Finishing";

                    Dictionary <string, object> filter = new Dictionary <string, object> {
                        { "storageName", storageName }, { "uom", "MTR" }, { "productCode", new Dictionary <string, object> {
                                                                                { "$in", products.ToArray() }
                                                                            } }
                    };
                    var response = httpClient.GetAsync($@"{APIEndpoint.Inventory}{inventorySummaryURI}filter=" + JsonConvert.SerializeObject(filter)).Result.Content.ReadAsStringAsync();
                    Dictionary <string, object> result = JsonConvert.DeserializeObject <Dictionary <string, object> >(response.Result);

                    var json = result.Single(p => p.Key.Equals("data")).Value;
                    List <InventorySummaryViewModel> inventorySummaries = JsonConvert.DeserializeObject <List <InventorySummaryViewModel> >(json.ToString());

                    foreach (MaterialDistributionNoteItemViewModel mdni in this.MaterialDistributionNoteItems)
                    {
                        int CountDetail = 0;

                        string materialDistributionNoteDetailError = "[";

                        foreach (MaterialDistributionNoteDetailViewModel mdnd in mdni.MaterialDistributionNoteDetails)
                        {
                            InventorySummaryViewModel inventorySummary = inventorySummaries.SingleOrDefault(p => p.productCode.Equals(mdnd.Product.code) && p.uom.Equals("MTR"));

                            materialDistributionNoteDetailError += "{";

                            if (inventorySummary == null)
                            {
                                CountDetail++;
                                materialDistributionNoteDetailError += "Product: 'Product is not exists in the storage', ";
                            }
                            else
                            {
                                if (mdnd.Quantity == null)
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "Quantity: 'Quantity is required', ";
                                }
                                else if (mdnd.Quantity <= 0)
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "Quantity: 'Quantity must be greater than zero', ";
                                }

                                if (mdnd.ReceivedLength == null)
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "ReceivedLength: 'Length is required', ";
                                }
                                else if (mdnd.ReceivedLength <= 0)
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "ReceivedLength: 'Length must be greater than zero', ";
                                }
                                else if (mdnd.ReceivedLength > inventorySummary.quantity)
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "ReceivedLength: 'Length must be less than or equal than stock', ";
                                }
                                else
                                {
                                    inventorySummary.quantity -= (double)mdnd.ReceivedLength;
                                }

                                if (mdnd.Supplier == null || string.IsNullOrWhiteSpace(mdnd.Supplier._id))
                                {
                                    CountDetail++;
                                    materialDistributionNoteDetailError += "Supplier: 'Supplier is required', ";
                                }
                            }

                            materialDistributionNoteDetailError += "}, ";
                        }

                        materialDistributionNoteDetailError += "]";

                        if (CountDetail > 0)
                        {
                            Count++;
                            materialDistributionNoteItemError += string.Concat("{ MaterialDistributionNoteDetails: ", materialDistributionNoteDetailError, " }, ");
                        }
                        else
                        {
                            materialDistributionNoteItemError += "{}, ";
                        }
                    }
                }

                materialDistributionNoteItemError += "]";

                if (Count > 0)
                {
                    yield return(new ValidationResult(materialDistributionNoteItemError, new List <string> {
                        "MaterialDistributionNoteItems"
                    }));
                }
            }
        }
Example #2
0
 public MaterialDistributionNoteReportController(MaterialDistributionNoteService materialDistributionNoteService)
 {
     this.materialDistributionNoteService = materialDistributionNoteService;
 }