public ShippingOrder ShipShippingOrder_ver1(string username_ad, string password_ad, int custid, string decoder_serial_number, string smartcard_serial_number, bool isInternet = false)
        {
            Authentication_class var_auth   = new Authentication_class();
            AuthenticationHeader authHeader = var_auth.getAuthHeader(username_ad, password_ad);

            AsmRepository.SetServiceLocationUrl(var_auth.var_service_location_url);

            var soService     = AsmRepository.AllServices.GetOrderManagementService(authHeader);
            var faService     = AsmRepository.AllServices.GetFinanceService(authHeader);
            var sbService     = AsmRepository.AllServices.GetSandBoxManagerService(authHeader);
            var agService     = AsmRepository.AllServices.GetAgreementManagementService(authHeader);
            var deviceService = AsmRepository.AllServices.GetDevicesService(authHeader);
            var viewfService  = AsmRepository.AllServices.GetViewFacadeService(authHeader);

            //msg = "Ship the shipping order...";
            //Console.WriteLine(msg);
            //Console.WriteLine("decoder sn : " + decoder_serial_number);
            //Console.WriteLine("smartcard sn : " + smartcard_serial_number);
            //logger.Info(msg);

            int d_tp    = 0;
            int s_tp    = 0;
            int d_model = 0;
            int s_model = 0;

            if (isInternet)
            {
                d_tp    = internet_router_tp_id;
                s_tp    = xl_sim_tp_id;
                d_model = router_model_id;
                s_model = xl_sim_model_id;
            }
            else
            {
                d_tp    = DECODER_TP_ID;
                s_tp    = SCTPID;
                d_model = DECODER_MODEL_ID;
                s_model = SC_MODEL_ID;
            }

            try
            {
                // get the pending shipping order and shipping order line
                ShippingOrder soc = soService.GetShippedOrders(custid, 0).Items.Find(t => t.StatusId == SOMAYSHIP);


                if (soc == null)
                {
                    Console.WriteLine("No Shipping Order with status May Ship, please check customer id : " + custid);
                    return(null);
                }

                // Get the hardware agreement detail of customer
                AgreementDetailView hardwaread = null;
                var hardwareads = viewfService.GetAgreementDetailView(new BaseQueryRequest()
                {
                    FilterCriteria = Op.Eq("DeviceIncluded", true) & Op.Eq("CustomerId", custid) & Op.Gt("Id", 0) & Op.IsNull("ProvisionedDevices"),
                    PageCriteria   = new PageCriteria(1),
                    SortCriteria   = new SortCriteriaCollection()
                    {
                        new SortCriteria()
                        {
                            Key           = "Id",
                            SortDirection = SortDirections.Descending
                        }
                    }
                });

                if (hardwareads.TotalCount == 0)
                {
                    //Console.WriteLine("Hardware product is not captured, can't ship the shipping order for customer id : " + custid);
                    return(null);
                }
                else
                {
                    hardwaread = hardwareads.Items[0];
                }

                // Find the shipping order lines for decoder and smartcard
                var decodersoline = soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp);
                var scsoline      = soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp);


                // Get random decoder and smartcard which are in stock, you should not use this since you have real device information.
                if (decoder_serial_number == "")
                {
                    decoder_serial_number = deviceService.GetDevices(
                        new BaseQueryRequest()
                    {
                        FilterCriteria = new CriteriaCollection()
                        {
                            new Criteria()
                            {
                                Key      = "ModelId",
                                Operator = Operator.Equal,
                                Value    = d_model.ToString()
                            },
                            new Criteria()
                            {
                                Key      = "StatusId",
                                Operator = Operator.Equal,
                                Value    = DEVICE_STATUS_STOCK
                            }
                        }
                    }).Items[0].SerialNumber;
                }
                else
                {
                    d_model = deviceService.GetDeviceBySerialNumber(decoder_serial_number).ModelId.Value;
                }


                if (smartcard_serial_number == "")
                {
                    smartcard_serial_number = deviceService.GetDevices(new BaseQueryRequest()
                    {
                        FilterCriteria = new CriteriaCollection()
                        {
                            new Criteria()
                            {
                                Key      = "ModelId",
                                Operator = Operator.Equal,
                                Value    = s_model.ToString()
                            },
                            new Criteria()
                            {
                                Key      = "StatusId",
                                Operator = Operator.Equal,
                                Value    = DEVICE_STATUS_STOCK
                            }
                        }
                    }).Items[0].SerialNumber;
                }
                else
                {
                    s_model = deviceService.GetDeviceBySerialNumber(smartcard_serial_number).ModelId.Value;
                }

                // Identify device info to the shipping order lines
                BuildList buildListdecoder = new BuildList
                {
                    OrderLineId     = decodersoline.Id,
                    OrderId         = soc.Id,
                    ModelId         = decodersoline.HardwareModelId.GetValueOrDefault(),
                    StockHandlerId  = soc.ShipFromStockHandlerId,
                    TransactionType = BuildListTransactionType.ShippingSerializedProducts
                };

                BuildList bldecoder = deviceService.CreateBuildList(buildListdecoder);

                BuildList buildListsc = new BuildList
                {
                    OrderLineId     = scsoline.Id,
                    OrderId         = soc.Id,
                    ModelId         = scsoline.HardwareModelId.GetValueOrDefault(),
                    StockHandlerId  = soc.ShipFromStockHandlerId,
                    TransactionType = BuildListTransactionType.ShippingSerializedProducts
                };

                BuildList blsc = deviceService.CreateBuildList(buildListsc);

                deviceService.AddDeviceToBuildListManually(bldecoder.Id.Value, decoder_serial_number);
                deviceService.AddDeviceToBuildListManually(blsc.Id.Value, smartcard_serial_number);

                //msg = "Starting perform build list";
                //Console.WriteLine(msg);
                //logger.Debug(msg);

                deviceService.PerformBuildListAction(bldecoder.Id.Value);
                deviceService.PerformBuildListAction(blsc.Id.Value);


                // Ship the Shipping Order
                //msg = "Starting link the device to customer";
                //Console.WriteLine(msg);
                //logger.Debug(msg);

                // Fill the agreement detail id on the shipping order line to link the device to customer
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp).AgreementDetailId = hardwaread.Id.Value;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp).AgreementDetailId = hardwaread.Id.Value;

                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp).ReceivedQuantity = 1;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp).ReceivedQuantity = 1;

                soService.ShipOrder(soc, shipso_reason, null);
                //msg = "Shipping Order :" + soc.Id.Value + " has been shipped!";
                //logger.Info(msg);


                return(soc);
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Inner Exception : " + ex.InnerException.Message);
                //msg = "Errors : " + ex.Message + "  ------  Exception Stack : " + ex.StackTrace;
                //Console.WriteLine("Errors : " + ex.Message);
                //Console.WriteLine("Stack : " + ex.StackTrace);
                //logger.Error(msg);

                return(null);
            }
        }
        public ShippingOrder ShipShippingOrder_ver2(String username_ad, String password_ad, int custid, string decoder_serial_number, string smartcard_serial_number, bool isInternet, string lnb_sn, string antenna_sn, int shippingorder_id, bool isHD)
        {
            Authentication_class var_auth   = new Authentication_class();
            AuthenticationHeader authHeader = var_auth.getAuthHeader(username_ad, password_ad);

            AsmRepository.SetServiceLocationUrl(var_auth.var_service_location_url);

            //var docmService = AsmRepository.GetServiceProxy<IDocumentManagementService>(authHeader);
            var soService     = AsmRepository.AllServices.GetOrderManagementService(authHeader);
            var faService     = AsmRepository.AllServices.GetFinanceService(authHeader);
            var sbService     = AsmRepository.AllServices.GetSandBoxManagerService(authHeader);
            var agService     = AsmRepository.AllServices.GetAgreementManagementService(authHeader);
            var deviceService = AsmRepository.AllServices.GetDevicesService(authHeader);
            var viewfService  = AsmRepository.AllServices.GetViewFacadeService(authHeader);

            //msg = "Ship the shipping order...";
            //Console.WriteLine(msg);
            //logger.Info(msg);

            int d_tp    = 0;
            int s_tp    = 0;
            int d_model = 0;
            int s_model = 0;

            int lnb_model     = 0;
            int antenna_model = 0;

            if (isInternet)
            {
                d_tp    = internet_router_tp_id;
                s_tp    = xl_sim_tp_id;
                d_model = router_model_id;
                s_model = xl_sim_model_id;
            }
            else
            {
                d_tp    = DECODER_TP_ID;
                s_tp    = SCTPID;
                d_model = DECODER_MODEL_ID;
                s_model = SC_MODEL_ID;
            }

            if (isHD)
            {
                //d_model = DECODER_HD_TP_ID;
                d_tp    = DECODER_HD_TP_ID;
                d_model = DECODER_HD_MODEL_ID;
            }

            try
            {
                // Get the hardware agreement detail of customer
                AgreementDetailView hardwaread = null;
                var hardwareads = viewfService.GetAgreementDetailView(new BaseQueryRequest()
                {
                    FilterCriteria = Op.Eq("DeviceIncluded", true) & Op.Eq("CustomerId", custid) & Op.Gt("Id", 0) & Op.IsNull("ProvisionedDevices"),
                    PageCriteria   = new PageCriteria(1),
                    SortCriteria   = new SortCriteriaCollection()
                    {
                        new SortCriteria()
                        {
                            Key           = "Id",
                            SortDirection = SortDirections.Descending
                        }
                    }
                });

                if (hardwareads.TotalCount == 0)
                {
                    //Console.WriteLine("Hardware product is not captured, can't ship the shipping order for customer id : " + custid);
                    return(null);
                }
                else
                {
                    hardwaread = hardwareads.Items[0];
                }

                // If not input the shipping order id then find the shipping order id by the random hardware product which don't assign device info yet.
                if (shippingorder_id == 0)
                {
                    shippingorder_id = getShippingOrderByHardware(username_ad, password_ad, hardwaread.Id.Value);
                    //Console.WriteLine("Ship the Shipping Order Id is : " + shippingorder_id);
                }
                // get the May shipping order and shipping order line( with smartcard or Sim card in it).
                var soc = soService.GetShippedOrders(custid, 0).Items.Find(t => (t.StatusId == SOMAYSHIP && t.Id.Value == shippingorder_id));


                if (soc == null)
                {
                    //Console.WriteLine("No Shipping Order with status May Ship, please check customer id : " + custid);
                    return(null);
                }



                // Find the shipping order lines for decoder and smartcard

                var scsoline = soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp);


                // Get random decoder and smartcard which are in stock, you should not use this since you have real device information.
                Device decoder   = null;
                Device smartcard = null;
                if (decoder_serial_number == "")
                {
                    var decoders = deviceService.GetDevices(
                        new BaseQueryRequest()
                    {
                        FilterCriteria = new CriteriaCollection()
                        {
                            new Criteria()
                            {
                                Key      = "ModelId",
                                Operator = Operator.Equal,
                                Value    = d_model.ToString()
                            },
                            new Criteria()
                            {
                                Key      = "StatusId",
                                Operator = Operator.Equal,
                                Value    = DEVICE_STATUS_STOCK
                            }
                        }
                    });
                    if (decoders.Items.Count == 0)
                    {
                        //Console.WriteLine("There are no decoder avaiable to use!");
                        return(null);
                    }
                    else
                    {
                        decoder_serial_number = decoders.Items[0].SerialNumber;
                    }
                }
                else
                {
                    decoder = deviceService.GetDeviceBySerialNumber(decoder_serial_number);
                    if (decoder.StatusId.Value == Int32.Parse(DEVICE_STATUS_STOCK) || decoder.StatusId.Value == Int32.Parse(DEVICE_STATUS_REFURSTOCK) ||
                        decoder.StatusId.Value == Int32.Parse(DEVICE_STATUS_REPAIREDSTOCK)
                        )
                    {
                    }
                    else
                    {
                        //Console.WriteLine(" Decoder with serial number " + decoder_serial_number + " is not in allowed capture status!");
                        return(null);
                    }
                }


                if (smartcard_serial_number == "")
                {
                    var smartcards = deviceService.GetDevices(new BaseQueryRequest()
                    {
                        FilterCriteria = new CriteriaCollection()
                        {
                            new Criteria()
                            {
                                Key      = "ModelId",
                                Operator = Operator.Equal,
                                Value    = s_model.ToString()
                            },
                            new Criteria()
                            {
                                Key      = "StatusId",
                                Operator = Operator.Equal,
                                Value    = DEVICE_STATUS_STOCK
                            }
                        }
                    });
                    if (smartcards.Items.Count == 0)
                    {
                        //Console.WriteLine("There are no smartcard avaiable to use!");
                        return(null);
                    }
                    else
                    {
                        smartcard_serial_number = smartcards.Items[0].SerialNumber;
                    }
                }
                else
                {
                    smartcard = deviceService.GetDeviceBySerialNumber(smartcard_serial_number);
                    if (smartcard.StatusId.Value == Int32.Parse(DEVICE_STATUS_STOCK) || smartcard.StatusId.Value == Int32.Parse(DEVICE_STATUS_REFURSTOCK) ||
                        smartcard.StatusId.Value == Int32.Parse(DEVICE_STATUS_REPAIREDSTOCK)
                        )
                    {
                    }
                    else
                    {
                        //Console.WriteLine(" Smartcard with serial number " + smartcard_serial_number + " is not in allowed capture status!");
                        return(null);
                    }
                }

                // Identify device info to the shipping order lines
                var dd = identifyDevice(username_ad, password_ad, soc, decoder_serial_number, d_tp, d_model);
                var sc = identifyDevice(username_ad, password_ad, soc, smartcard_serial_number, s_tp, s_model);
                if (lnb_sn.Length > 0)
                {
                    var lnb = deviceService.GetDeviceBySerialNumber(lnb_sn);
                    if (lnb.StatusId.Value == Int32.Parse(DEVICE_STATUS_STOCK) || lnb.StatusId.Value == Int32.Parse(DEVICE_STATUS_REFURSTOCK) ||
                        lnb.StatusId.Value == Int32.Parse(DEVICE_STATUS_REPAIREDSTOCK)
                        )
                    {
                        lnb_model = lnb.ModelId.Value;
                    }
                    else
                    {
                        //Console.WriteLine(" LNB with serial number " + lnb_sn + " is not in allowed capture status!");
                        //return null;
                    }
                    identifyDevice(username_ad, password_ad, soc, lnb_sn, lnb_tp_id, lnb.ModelId.Value);
                }
                if (antenna_sn.Length > 0)
                {
                    var antenna = deviceService.GetDeviceBySerialNumber(lnb_sn);
                    if (antenna.StatusId.Value == Int32.Parse(DEVICE_STATUS_STOCK) || antenna.StatusId.Value == Int32.Parse(DEVICE_STATUS_REFURSTOCK) ||
                        antenna.StatusId.Value == Int32.Parse(DEVICE_STATUS_REPAIREDSTOCK)
                        )
                    {
                        antenna_model = antenna.ModelId.Value;
                    }
                    else
                    {
                        //Console.WriteLine(" Antenna with serial number " + antenna_sn + " is not in allowed capture status!");
                        //return null;
                    }
                    identifyDevice(username_ad, password_ad, soc, antenna_sn, antenna_tp_id, antenna.ModelId.Value);
                }

                if (dd == null || sc == null)
                {
                    return(null);
                }

                //msg = "Starting perform build list";
                //Console.WriteLine(msg);
                //logger.Debug(msg);


                // Ship the Shipping Order
                //msg = "Starting link the device to customer";
                //Console.WriteLine(msg);
                //logger.Debug(msg);

                // Fill the agreement detail id on the shipping order line to link the device to customer
                //soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp).AgreementDetailId = hardwaread.Id.Value;
                //soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp).AgreementDetailId = hardwaread.Id.Value;

                // Fill the correct model id
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp).HardwareModelId          = d_model;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp).HardwareModelId          = s_model;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == lnb_tp_id).HardwareModelId     = lnb_model;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == antenna_tp_id).HardwareModelId = antenna_model;

                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == d_tp).ReceivedQuantity = 1;
                soc.ShippingOrderLines.Items.Find(t => t.TechnicalProductId == s_tp).ReceivedQuantity = 1;

                soService.ShipOrder(soc, shipso_reason, null);
                //msg = "Shipping Order :" + soc.Id.Value + " has been shipped!";
                //logger.Info(msg);


                return(soc);
            }
            catch (Exception ex)
            {
                //Console.WriteLine("Inner Exception : " + ex.InnerException.Message);
                //msg = "Errors : " + ex.Message + "  ------  Exception Stack : " + ex.StackTrace;
                //Console.WriteLine("Errors : " + ex.Message);
                //Console.WriteLine("Stack : " + ex.StackTrace);
                //logger.Error(msg);

                return(null);
            }
        }