public override bool HandleSaveTask()
        {
            bool success = false;

            try
            {
                IrregularOrder order = SelectedOrder;
                if (order != null && ValidateInput())
                {
                    Constants.PaymentStatus currentPaymentStatus = EnumHelper.Parse <Constants.PaymentStatus>(order.payment_status);
                    // Collects order information
                    Collection <OrderItem> orderItems = new Collection <OrderItem>();
                    order.sender_name          = tbSenderName.Text;
                    order.sender_phone         = tbSenderPhoneNumber.Text;
                    order.sender_id_card_no    = tbSenderIDCardNo.Text;
                    order.sender_address       = tbSenderAddress.Text;
                    order.recipient_name       = tbRecipientName.Text;
                    order.recipient_phone      = tbRecipientPhoneNumber.Text;
                    order.recipient_id_card_no = tbRecipientIDCardNo.Text;
                    order.recipient_address    = tbRecipientAddress.Text;
                    order.tour_id        = cboTour.SelectedValue.ToString();
                    order.order_status   = CurrentDeliveryStatus.ToString();
                    order.payment_status = CurrentPaymentStatus.ToString();
                    int     totalQuantity = 0;
                    decimal totalValue    = 0;
                    decimal totalCost     = 0;
                    foreach (var item in ucItemsList.OrderItems)
                    {
                        OrderItem newOrderItem = new OrderItem()
                        {
                            id                = IDGenerator.NewOrderItemId(order.id, orderItems.Count + 1),
                            order_id          = order.id,
                            name              = item.name,
                            weight            = item.weight,
                            size              = item.size,
                            quantity          = item.quantity,
                            value             = item.value,
                            description       = item.description,
                            cost              = item.cost,
                            order_item_number = item.order_item_number
                        };
                        orderItems.Add(newOrderItem);
                        totalQuantity += item.quantity;
                        totalValue    += item.value * item.quantity;
                        totalCost     += item.cost;
                    }
                    order.total_quantity = totalQuantity;
                    order.total_value    = totalValue;
                    order.total_cost     = decimal.Parse(tbTotalCost.Text);
                    if (rbtnDeliveryStatus_Closed.Checked)
                    {
                        order.closed_date = DateTime.Now;
                    }

                    if (currentPaymentStatus == Constants.PaymentStatus.Unpaid &&
                        rbtnPaymentStatus_Paid.Checked &&
                        (new IrregularOrderPaymentView(order, orderItems)).ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }
                    string result = _business.Update(order, orderItems);
                    if (string.IsNullOrEmpty(result))
                    {
                        MessageBox.Show("Đơn hàng đã cập nhật thành công.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
                        ChangeOrderDetailsEditingStatus(false);
                        _isEditing = false;
                        success    = true;
                        DataBind();
                        PopulateOrderDetails(order);
                    }
                    else
                    {
                        MessageBox.Show(result, Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                AppLogger.logError(this.ToString(), "Error occurs when saving order changes.", ex);
                MessageBox.Show("Có lỗi xảy ra trong khi cập nhập đơn hàng.", Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(success);
        }
Example #2
0
        private void btnCreateOrder_Click(object sender, EventArgs e)
        {
            if (ValidateInput() &&
                MessageBox.Show(Constants.Messages.CREATE_ORDER_CONFIRMATION_MESSAGE, Constants.Messages.CREATE_ORDER_CONFIRMATION_CAPTION, MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                bool   success    = true;
                string saveResult = string.Empty;
                try
                {
                    // Creates OrderBase instance and gets general info
                    OrderBase order = new OrderBase();
                    order.id = rbtnOrderType_Regular.Checked ? IDGenerator.NewId <RegularOrder>() : IDGenerator.NewId <IrregularOrder>();
                    AppLogger.logDebug(this.ToString(), string.Format("Id: {0}", order.id));
                    order.payment_status = this.rbtnPaymentStatus_Paid.Checked ? Constants.PaymentStatus.Paid.ToString() : Constants.PaymentStatus.Unpaid.ToString();
                    order.order_status   = Constants.DeliveryStatus.Waiting.ToString();
                    order.tour_id        = cboDestination.SelectedValue.ToString();
                    order.created_date   = DateTime.Now;
                    order.created_by     = SystemParam.CurrentUser.user_name;
                    int     totalQuantity = 0;
                    decimal totalValue    = 0;
                    decimal totalCost     = 0;
                    foreach (var item in ucItemsList.OrderItems)
                    {
                        item.id        = IDGenerator.NewOrderItemId(order.id, ucItemsList.OrderItems.IndexOf(item) + 1);
                        item.order_id  = order.id;
                        totalQuantity += item.quantity;
                        totalValue    += item.value * item.quantity;
                        totalCost     += item.cost;
                    }
                    order.total_quantity = totalQuantity;
                    order.total_value    = totalValue;
                    order.total_cost     = totalCost;

                    // Convert order info per order type
                    if (rbtnOrderType_Regular.Checked)
                    {
                        RegularOrder regularOrder = order.ToRegular();
                        regularOrder.sender_id    = Sender.id;
                        regularOrder.recipient_id = Recipient.id;

                        saveResult = _regularBusiness.Insert(regularOrder, ucItemsList.OrderItems);
                    }
                    else
                    {
                        IrregularOrder irregularOrder = order.ToIrregular();
                        irregularOrder.sender_name          = tbSenderName.Text;
                        irregularOrder.sender_phone         = tbSenderPhoneNumber.Text;
                        irregularOrder.sender_id_card_no    = tbSenderIDCardNo.Text;
                        irregularOrder.sender_address       = tbSenderAddress.Text;
                        irregularOrder.recipient_name       = tbRecipientName.Text;
                        irregularOrder.recipient_phone      = tbRecipientPhoneNumber.Text;
                        irregularOrder.recipient_id_card_no = tbRecipientIDCardNo.Text;
                        irregularOrder.recipient_address    = tbRecipientAddress.Text;

                        if (rbtnPaymentStatus_Paid.Checked &&
                            (new IrregularOrderPaymentView(irregularOrder, ucItemsList.OrderItems)).ShowDialog() != System.Windows.Forms.DialogResult.OK)
                        {
                            AppLogger.logDebug(this.ToString(), "Payment process cancelled.");
                            return;
                        }
                        saveResult = _irregularBusiness.Insert(irregularOrder, ucItemsList.OrderItems);
                    }
                }
                catch (Exception ex)
                {
                    AppLogger.logError(this.ToString(), ex.Message, ex);
                    success = false;
                }
                if (success && string.IsNullOrEmpty(saveResult))
                {
                    AppLogger.logInfo(this.ToString(), "Finish inserting new order into database.");
                    MessageBox.Show("Đã tạo thành công đơn hàng mới.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    if (string.IsNullOrEmpty(saveResult))
                    {
                        saveResult = Constants.Messages.CREATE_ORDER_ERROR_MESSAGE;
                    }
                    MessageBox.Show(saveResult, Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }