private void ToolStripMenuItemDelDeliveryNote_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确认刪除该进货单么?\r\n。",
                                "确认删除?",
                                MessageBoxButtons.YesNo,
                                MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            int    rowIdx         = this.pagedDgvDeliveryNote.DgvData.CurrentRow.Index;
            String deliveryNoteId = this.pagedDgvDeliveryNote.DgvData.Rows[rowIdx].Cells[0].Value.ToString();

            DeliveryNoteType note = DeliveryNoteDAL.GetOneDeliveryNote(deliveryNoteId);

            if (note == null)
            {
                return;
            }

            // Restore the stock.
            String tranIdStr = note.DeliveryOrderIds;

            String[] tranIds      = tranIdStr.Split(new char[] { ',', ' ' });
            String   promptString = "";

            foreach (String tranId in tranIds)
            {
                EbayTransactionType trans = EbayTransactionDAL.GetOneTransactonById(tranId);
                if (trans == null || trans.ItemSKU == null || trans.ItemSKU == "")
                {
                    MessageBox.Show(String.Format("订单号{0}异常", tranId), "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    continue;
                }

                String            itemSku      = trans.ItemSKU;
                int               saleQuantity = trans.SaleQuantity;
                InventoryItemType item         = ItemDAL.GetItemBySKU(itemSku);
                if (item == null)
                {
                    MessageBox.Show(String.Format("无此sku商品{0}", itemSku), "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    continue;
                }

                ItemDAL.IncreaseItem(itemSku, saleQuantity);
                promptString += String.Format("\nsku:{0} 原库存 {1} => 现库存 {2}", itemSku, item.ItemStockNum, item.ItemStockNum + saleQuantity);

                //
                // Update transaction delivery status.
                //
                EbayTransactionDAL.UpdateTransactionDeliveryStatus(tranId, false, -1);
            }

            DeliveryNoteDAL.DeleteOneDeliveryNote(deliveryNoteId);

            pagedDgvDeliveryNote.LoadData();

            // Indicate main form to update view.
            Deleted = true;

            MessageBox.Show(String.Format("删除发货单成功 {0}", promptString),
                            "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void CreateDeliveryNote()
        {
            if (mEditMode != EditMode.CreatNew && mEditMode != EditMode.CreateFromTrans)
            {
                // We are not creating a new delivery note.
                return;
            }

            // Sanity check.
            if (mDtTransactions == null || mDtTransactions.Rows.Count == 0)
            {
                return;
            }

            //
            // Add a new delivery note.
            //

            // tranIds will be like "2264,2265,2266"
            String tranIds  = "";
            bool   firstRow = true;

            foreach (DataRow dr in mDtTransactions.Rows)
            {
                String tranIdLoc = StringUtil.GetSafeString(dr["TransactionId"]);
                if (tranIdLoc == null || tranIdLoc == "")
                {
                    continue;
                }

                if (!firstRow)
                {
                    tranIds += ",";
                }
                else
                {
                    firstRow = false;
                }

                tranIds += tranIdLoc;
            }

            double fee      = 0.0;
            double extraFee = 0.0;

            Double.TryParse(textBoxFee.Text, out fee);
            Double.TryParse(textBoxExtraFee.Text, out extraFee);

            DeliveryNoteType deliveryNote = new DeliveryNoteType();

            deliveryNote.DeliveryDate     = DateTime.Now;
            deliveryNote.DeliveryOrderIds = tranIds;
            deliveryNote.DeliveryUser     = "";
            deliveryNote.DeliveryFee      = fee;
            deliveryNote.DeliveryExtraFee = extraFee;
            deliveryNote.DeliveryComment  = textBoxComment.Text;

            // Decrease the stock.
            // Two runs:
            //  first run check validity.
            //  second run do the actual stock decreament.
            // This is to ensure the data integrity.
            Dictionary <String, int> itemSkuToTotalDecreased = new Dictionary <string, int>();

            String stockChangePrompt = "";

            for (int ii = 0; ii < 2; ++ii)
            {
                foreach (DataRow dr in mDtTransactions.Rows)
                {
                    String tranId       = StringUtil.GetSafeString(dr["TransactionId"]);
                    String itemSku      = StringUtil.GetSafeString(dr["ItemSKU"]);
                    int    saleQuantity = StringUtil.GetSafeInt(dr["SaleQuantity"]);

                    if (0 == ii)
                    {
                        if (itemSku == "")
                        {
                            MessageBox.Show(String.Format("订单{0}没有关联商品", tranId),
                                            "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        InventoryItemType item = ItemDAL.GetItemBySKU(itemSku);
                        if (item == null)
                        {
                            MessageBox.Show(String.Format("商品不存在, sku={0}", itemSku),
                                            "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (!itemSkuToTotalDecreased.ContainsKey(itemSku))
                        {
                            itemSkuToTotalDecreased.Add(itemSku, saleQuantity);
                        }
                        else
                        {
                            itemSkuToTotalDecreased[itemSku] += saleQuantity;
                        }

                        if (item.ItemStockNum < itemSkuToTotalDecreased[itemSku])
                        {
                            MessageBox.Show(String.Format("商品{0}库存不足,实际库存{1} < 售出数{2}",
                                                          itemSku, item.ItemStockNum, itemSkuToTotalDecreased[itemSku]),
                                            "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (1 == ii)
                    {
                        InventoryItemType item = ItemDAL.GetItemBySKU(itemSku);
                        int origStock          = item.ItemStockNum;

                        if (!ItemDAL.DecreaseItem(itemSku, saleQuantity))
                        {
                            MessageBox.Show(String.Format("更新库存失败:商品{0}库存不足销售数量{1}", itemSku, saleQuantity),
                                            "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        stockChangePrompt += String.Format("\nsku: {0}, 原库存 {1} => 现库存 {2}",
                                                           itemSku, origStock, origStock - saleQuantity);
                    }
                }
            } // End of two runs

            //
            // Create a new delivery note.
            //
            int deliveryNoteId = -1;

            if ((deliveryNoteId = DeliveryNoteDAL.InsertOneDeliveryNote(deliveryNote)) <= 0)
            {
                MessageBox.Show("创建发货单失败", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //
            // Update transaction delivery status.
            //
            foreach (DataRow dr in mDtTransactions.Rows)
            {
                String tranId = StringUtil.GetSafeString(dr["TransactionId"]);
                EbayTransactionDAL.UpdateTransactionDeliveryStatus(tranId, true, deliveryNoteId);
            }

            // Indicate main form to update data.
            Added = true;

            MessageBox.Show(String.Format("创建发货单成功 {0}", stockChangePrompt),
                            "恭喜", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }