private string GetSupplierCode(string City)
        {
            if (City == null)
            {
                return(defalutCode);
            }
            string sCode = "";

            //生成供应商编号逻辑:
            //1:根据city找城市区号
            //2:如果找不到区号使用默认的区号9999
            //3:去数据库中查询供应商编号以这个区号开头的有几条记录,
            //供应商编号 = 城市区号+(记录数+1)
            sCode = CityCodes.Where(x => City.Contains(x.Name)).Select(x => x.Zip).FirstOrDefault();
            if (string.IsNullOrEmpty(sCode))
            {
                sCode = defalutCode;
            }

            int count = db.SupplierMng.Where(x => x.SupplierCode.StartsWith(sCode)).Count();

            sCode += (count + 1).ToString().PadLeft(4, '0');
            return(sCode);
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            if (!rdoYto.Checked && !rdoSf.Checked)
            {
                MessageBox.Show(this, "选择快递公司先.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (btnPrint.Text.Equals("Printed"))
            {
                DialogResult dr = MessageBox.Show(this, "已打印, 是否再次打印?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (DialogResult.No == dr)
                {
                    return;
                }
            }

            if (string.IsNullOrEmpty(txtProducts.Text))
            {
                DialogResult dr = MessageBox.Show(this, "产品信息为空, 是否继续打印?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (DialogResult.No == dr)
                {
                    return;
                }
            }

            //if (rdoSf.Checked)
            //    MessageBox.Show(this, "发顺丰!\n确认是否顺丰面单!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            string          printerDocumentName = string.Format("{0}({1})", txtRecipientName.Text, txtBuyerAccount.Text);
            BillPrinterBase printer;

            if (rdoYto.Checked)             // yto
            {
                printer             = new YtoPrinter(printerDocumentName);
                printer.PrinterName = Settings.Instance.YtoPrinter;
            }
            else             // sf
            {
                string destCityCode = string.Empty;
                if (chkSfOldBill.Checked)
                {
                    printer             = new SfPrinter(printerDocumentName);
                    printer.PrinterName = Settings.Instance.SfPrinter;

                    destCityCode = CityCodes.GetCityCode(txtProvince.Text.Trim());
                    if (string.IsNullOrEmpty(destCityCode))
                    {
                        destCityCode = CityCodes.GetCityCode(txtCity1.Text.Trim());
                    }
                    ((SfPrinter)printer).DestCode = destCityCode;
                }
                else
                {
                    SfOrder sforder = null;
                    if (chkAutoSfBillNumber.Checked)
                    {
                        sforder               = new SfOrder();
                        sforder.Province      = txtProvince.Text.Trim();
                        sforder.City1         = txtCity1.Text.Trim();
                        sforder.City2         = txtCity2.Text.Trim();
                        sforder.District      = txtDistrict.Text.Trim();
                        sforder.StreetAddress = txtStreetAddress.Text.Trim();
                        sforder.RecipientName = txtRecipientName.Text.Trim();
                        sforder.Mobile        = txtMobile.Text.Trim();
                        sforder.Phone         = txtPhone.Text.Trim();

                        // random order if for sf service.
                        string randomOrderId = new Random(Environment.TickCount).Next(100000000, 999999999).ToString();

                        //  check arrivable.
                        bool arrivable = sforder.Arrivable(randomOrderId);                        //_orders[0].OrderId);
                        if (!string.IsNullOrEmpty(sforder.ErrorCode))
                        {
                            MessageBox.Show(
                                this,
                                string.Format("筛单时发生错误: 错误代码={0}, 错误信息={1}", sforder.ErrorCode, sforder.ErrorMessage),
                                this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }
                        if (!arrivable)
                        {
                            DialogResult dr = MessageBox.Show(
                                this,
                                string.Format("此地址顺丰不到, 可能需要收件人自提.\n{0}\n\n是否继续发顺丰?",
                                              string.Format("{0} {1} {2} {3} {4}", txtProvince.Text.Trim(), txtCity1.Text.Trim(), txtCity2.Text.Trim(), txtDistrict.Text.Trim(), txtStreetAddress.Text.Trim())),
                                this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                            if (DialogResult.No == dr)
                            {
                                return;
                            }
                        }

                        sforder.RunOrderService(randomOrderId);                        //_orders[0].OrderId);
                        if (!string.IsNullOrEmpty(sforder.ErrorCode))
                        {
                            MessageBox.Show(
                                this,
                                string.Format("获取顺丰单号时发生错误: 错误代码={0}, 错误信息={1}", sforder.ErrorCode, sforder.ErrorMessage),
                                this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }

                        // Got mail number but no dest code. Address may be invalid.
                        if (string.IsNullOrEmpty(sforder.DestCode))
                        {
                            MessageBox.Show(
                                this,
                                string.Format("未获得目标地区代码(DestCode).\n请检查是否地址有误并重新打印."),
                                this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }

                        txtBillNumber.Text = sforder.MailNumber;
                        txtDestCode.Text   = sforder.DestCode;
                    }
                    else
                    {
                        if (txtBillNumber.Text.Length < 12)
                        {
                            MessageBox.Show(
                                this,
                                "请手动输入正确的顺丰单号.",
                                this.Text,
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            txtBillNumber.Focus();
                            return;
                        }
                    }

                    printer             = new SfBillNewPrinter(txtBillNumber.Text, printerDocumentName);
                    printer.PrinterName = Settings.Instance.SfNewPrinter;
                    ((SfBillNewPrinter)printer).DestCode = txtDestCode.Text;

                    // Calculate total count to display on bill.
                    ((SfBillNewPrinter)printer).ItemAmount = GetProductTotalCount();
                }

                ((SfPrinter)printer).IsFreightCollect = chkFriehghtCollect.Checked;
                ((SfPrinter)printer).IsPickup         = chkPickup.Checked;
            }

            string senderName = txtSenderName.Text;

            if (txtSenderName.Text.Equals("德 国 e 购") || txtSenderName.Text.Equals("buy欧洲"))
            {
                senderName = string.IsNullOrEmpty(txtSenderName.Text) ? string.Empty : string.Format("{0}****", txtSenderName.Text.Substring(0, 1));
            }
            string buyerAccount = string.IsNullOrEmpty(txtBuyerAccount.Text) ? string.Empty : string.Format("{0}***{1}", txtBuyerAccount.Text.Substring(0, 1), txtBuyerAccount.Text.Substring(txtBuyerAccount.Text.Length - 1, 1));

            printer.Font                = new Font("Microsoft Yahei", 11);
            printer.SenderName          = senderName;    //txtSenderName.Text.Trim();
            printer.SenderAd            = txtSenderAd.Text.Trim();
            printer.SenderTel           = txtSenderTel.Text.Trim();
            printer.DisplayBuyerAccount = false;            //(txtSenderName.Text.Trim().Equals(ShopProfile.Current.DisplayNameOnBill));
            printer.RecipientName       = txtRecipientName.Text.Trim();
            printer.BuyerAccount        = buyerAccount;     //txtBuyerAccount.Text.Trim();
            printer.Province            = txtProvince.Text;
            printer.City1               = txtCity1.Text;
            printer.City2               = txtCity2.Text;
            printer.District            = txtDistrict.Text;
            printer.StreetAddress       = txtStreetAddress.Text;
            printer.Mobile              = txtMobile.Text.Trim();
            printer.Phone               = txtPhone.Text.Trim();
            printer.HolidayDelivery     = chkHoliday.Checked;
            printer.ProductInfos        = txtProducts.Lines;
            printer.Print();
            _printed = true;

            #region obsoleted code on 2015/01/09
            //Size docSize = new Size(1000, 1000);
            //if (rdoYto.Checked)
            //    docSize = new Size(900, 550);
            //else if (rdoSf.Checked)
            //    docSize = new Size(780, 550);

            //PrintDocument pdoc = new PrintDocument();
            //pdoc.DocumentName = string.Format("{0}({1})", txtRecipientName.Text, txtBuyerAccount.Text);
            //pdoc.DefaultPageSettings.PaperSize = new PaperSize("custom", docSize.Width, docSize.Height);
            //pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
            //pdoc.Print();
            #endregion

            this.Focus();
            txtBillNumber.Focus();
            btnPrint.Text      = "Printed";
            btnPrint.ForeColor = Color.OrangeRed;
            this.Activate();
            Cursor.Current = Cursors.Default;

            btnGo.Enabled = true;
        }