internal bool IsCustomerValid(string customerName)
 {
     _Customer = _CustomerRepository.GetByName(customerName);
     if (_Customer.ID <= 0)
     {
         return(false);
     }
     return(true);
 }
        public CreatePartInvoiceResult CreatePartInvoice(string stockCode, int quantity, string customerName)
        {
            // This method calls to external services (Database, Webservice). It is better to have try catch to handle exceptions.
            try
            {
                // Changed: Add validation for customerName.
                if (string.IsNullOrEmpty(stockCode) || string.IsNullOrEmpty(customerName))
                {
                    return(new CreatePartInvoiceResult(false));
                }

                if (quantity <= 0)
                {
                    return(new CreatePartInvoiceResult(false));
                }

                Customer _Customer = __CustomerRepositoryDB.GetByName(customerName);
                // Changed: Validate Customer in case of customer not found.
                if (_Customer == null || _Customer.ID <= 0)
                {
                    return(new CreatePartInvoiceResult(false));
                }

                int _Availability = __IPartAvailabilityService.GetAvailability(stockCode);
                if (_Availability <= 0)
                {
                    return(new CreatePartInvoiceResult(false));
                }

                PartInvoice _PartInvoice = new PartInvoice
                {
                    StockCode  = stockCode,
                    Quantity   = quantity,
                    CustomerID = _Customer.ID
                };

                __PartInvoiceRepositoryDB.Add(_PartInvoice);

                return(new CreatePartInvoiceResult(true));
            }
            catch (Exception)
            {
                // Logging
                return(new CreatePartInvoiceResult(false));
            }
        }
        public CreatePartInvoiceResult CreatePartInvoice(string stockCode, int quantity, string customerName)
        {
            if (IsInValidRequest(stockCode, quantity))
            {
                return(new CreatePartInvoiceResult(false));
            }

            Customer _Customer = CustomerRepository.GetByName(customerName);

            if (_Customer.ID <= 0)
            {
                return(new CreatePartInvoiceResult(false));
            }

            AddPartInvoiceToRepository(stockCode, quantity, _Customer.ID);

            return(new CreatePartInvoiceResult(true));
        }