示例#1
0
func (p *OrderPrint) Setup() *exit.Exit {
	order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
	if err != nil {
		panic(err.Error())
	}

	p.Order = order
	if p.Customer, err = service.Person.GetPersonById(p.Order.CustomerId); err != nil {
		panic(err)
	} else if p.Customer == nil {
		panic("Customer does not exist!")
	}

	if p.First == false {
		needprice := person_need_price(p.Customer.Id)
		if needprice {
			// Redirect to print no pirce page.
			return exit.Redirect(fmt.Sprintf("/order/printnoprice/%d", p.TrackNumber))
		}
	}

	p.ShowPicture = showPicture()

	p.Sumprice = p.sumprice()
	return nil
}
示例#2
0
// before submit, here url injection is ready but post data is not
// injected. we get order from db.
func (p *ShippingInstead) OnPrepareForSubmit() {
	var err error
	if p.Order, err = orderservice.GetOrderByTrackingNumber(p.TrackNumber); err != nil {
		panic(err.Error())
	}
	p.CustomerId = p.Order.CustomerId
}
示例#3
0
func (p *ShippingInsteadPrint) Setup() {
	p.productcache = map[int]*model.Product{}

	order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
	if err != nil {
		panic(err.Error())
	}
	p.Order = order
	if p.Customer, err = service.Person.GetPersonById(p.Order.CustomerId); p.Customer == nil {
		panic("Customer does not exist!")
	}

	// killthis ?
	// logic: update order's accumulated
	if p.Order.Accumulated != -p.Customer.AccountBallance {
		p.Order.Accumulated = -p.Customer.AccountBallance
		_, err := service.Order.UpdateOrder(p.Order)
		if err != nil {
			panic(err.Error())
		}
	}
	// kill this?
	p.Sumprice = p.sumprice()

	// init suborders
	subOrders, err := orderservice.LoadSubOrders(p.Order)
	if err != nil {
		panic(err.Error())
	}
	p.SubOrders = subOrders

	// TODO use calculate or use value in db?
	// calculate statistics to parent order
	//   f(x) = Sum(suborder.quantity * unit-price + order.expressfee)
	//
	var totalPrice float64 = 0
	var totalExpressFee int64 = 0
	var totalCount int = 0
	for _, so := range subOrders {
		totalCount += so.TotalCount
		totalPrice += so.SumOrderPrice()
		if so.ExpressFee > 0 {
			totalExpressFee += so.ExpressFee
		}
	}
	p.Order.TotalCount = totalCount
	p.Order.TotalPrice = totalPrice
	p.Order.ExpressFee = totalExpressFee

}
示例#4
0
func (p *ViewOrder) Setup() {
	order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
	if err != nil {
		panic(err.Error())
	}
	p.Order = order
	p.Customer, err = service.Person.GetPersonById(p.Order.CustomerId)
	if err != nil {
		panic(err)
	}
	if p.Customer == nil {
		panic(fmt.Sprintf("customer not found: id: %v", p.Order.CustomerId))
	}
}
示例#5
0
func (p *OrderPrintNoPrice) Setup() {
	order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
	if err != nil {
		panic(err.Error())
	}

	p.Order = order
	if p.Customer, err = service.Person.GetPersonById(p.Order.CustomerId); err != nil {
		panic(err)
	} else if p.Customer == nil {
		panic("Customer does not exist!")
	}

	p.Sumprice = p.sumprice()
}
func ordersJsonByTrackNumbers(tns string) (string, string) {
	orders := []*model.Order{}
	pieces := strings.Split(tns, ",")
	for _, piece := range pieces {
		tn, err := strconv.ParseInt(strings.Trim(piece, " "), 10, 64)
		if err != nil {
			panic(err.Error())
		}
		order, err := orderservice.GetOrderByTrackingNumber(tn)
		if err != nil {
			panic(err.Error())
		}
		orders = append(orders, order)
	}
	return toJsonList(orders)
}
示例#7
0
func (p *ShippingInstead) Setup() {
	if p.TrackNumber == 0 {
		panic("Can't find order!")
	}
	p.productcache = map[int]*model.Product{}

	// edit mode
	var err error
	if p.Order, err = orderservice.GetOrderByTrackingNumber(p.TrackNumber); err != nil {
		panic(err.Error())
	}
	p.CustomerId = p.Order.CustomerId

	// init person
	p.Customer = personservice.GetCustomer(p.CustomerId)
	if p.Customer == nil {
		panic(fmt.Sprintf("customer not found: id: %v", p.CustomerId))
	}

	// init suborders
	subOrders, err := orderservice.LoadSubOrders(p.Order)
	if err != nil {
		panic(err.Error())
	}
	p.SubOrders = subOrders

	// calculate statistics to parent order
	//   f(x) = Sum(suborder.quantity * unit-price + order.expressfee)
	//
	var totalPrice float64 = 0
	var totalExpressFee int64 = 0
	var totalCount int = 0
	for _, so := range subOrders {
		totalCount += so.TotalCount
		totalPrice += so.TotalPrice //SumOrderPrice()
		if so.ExpressFee > 0 {
			totalExpressFee += so.ExpressFee
		}
	}
	p.Order.TotalCount = totalCount
	p.Order.TotalPrice = totalPrice
	p.Order.ExpressFee = totalExpressFee
}
示例#8
0
// **** important logic ****
// when close order. 结款, Close Order
func (p *ButtonSubmitHere) OnSuccessFromCloseForm() *exit.Exit {
	fmt.Println("\n\n\n>>>>>>>>>>>>>>>>>>>> On success from close order.................", p.TrackNumber)

	// 1/2 update delivery informantion to order.
	order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
	if err != nil {
		panic(err.Error())
	}
	order.Status = "done"
	_, err = service.Order.UpdateOrder(order)
	if err != nil {
		panic(err.Error())
	}

	// 2/2 update customer's AccountBallance
	customer, err := service.Person.GetPersonById(order.CustomerId)
	if err != nil {
		panic(err)
	}
	if customer == nil {
		panic(fmt.Sprintf("Customer not found for order! id %v", order.CustomerId))
	}
	customer.AccountBallance += p.Money
	if _, err = personservice.Update(customer); err != nil {
		panic(err.Error())
	}

	// create chagne log at the same time:
	accountdao.CreateAccountChangeLog(&model.AccountChangeLog{
		CustomerId:     customer.Id,
		Delta:          p.Money,
		Account:        customer.AccountBallance,
		Type:           3, // batch close order.
		RelatedOrderTN: order.TrackNumber,
		Reason:         "",
	})
	return route.RedirectDispatch(p.Referer, "/order/list")
}
示例#9
0
// **** important logic ****
// TODO transaction. Move to right place. 发货
func (p *ButtonSubmitHere) OnSuccessFromDeliverForm() *exit.Exit {

	var expressFee int64 = 0
	if p.DaoFu == "on" {
		// if order.ExpressFee == -1, means this is `daofu`, don't add -1 to 累计欠款.
		// TODO add field isDaofu to order table. Change ExpressFee to 0;
		expressFee = -1
	} else {
		expressFee = p.ExpressFee
	}

	if _, err := service.Order.DeliverOrder(
		p.TrackNumber, p.DeliveryTrackingNumber, p.DeliveryMethod, expressFee); err != nil {
		panic(err)
	}
	return route.RedirectDispatch(p.Referer, "/order/list")

	if false { // backup, has been replace with above.

		/////////////

		// 1/2 update delivery informantion to order.

		// 1. get order form db.
		order, err := orderservice.GetOrderByTrackingNumber(p.TrackNumber)
		if err != nil {
			panic(err.Error())
		}

		// 2. set data back to order.
		order.DeliveryTrackingNumber = p.DeliveryTrackingNumber
		order.DeliveryMethod = p.DeliveryMethod
		if p.DaoFu == "on" {
			// if order.ExpressFee == -1, means this is `daofu`, don't add -1 to 累计欠款.
			// TODO add field isDaofu to order table. Change ExpressFee to 0;
			order.ExpressFee = -1
		} else {
			order.ExpressFee = p.ExpressFee
		}
		order.Status = "delivering"

		// 3. get person, check if customer exists.
		customer, err := service.Person.GetPersonById(order.CustomerId)
		if err != nil {
			panic(err)
		} else if customer == nil {
			panic(fmt.Sprintf("Customer not found for order! id %v", order.CustomerId))
		}

		// 4. the last chance to update accumulated.
		order.Accumulated = -customer.AccountBallance

		// 5. save order changes.
		if _, err := service.Order.UpdateOrder(order); err != nil {
			panic(err.Error())
		}

		// 6. update customer's AccountBallance
		switch model.OrderType(order.Type) {
		case model.Wholesale, model.SubOrder: // 代发不参与, 代发订单由其子订单负责参与累计欠款的统计;
			customer.AccountBallance -= order.TotalPrice
			if order.ExpressFee > 0 {
				customer.AccountBallance -= float64(order.ExpressFee)
			}
			if _, err = personservice.Update(customer); err != nil {
				panic(err.Error())
			}

			// create chagne log.
			accountdao.CreateAccountChangeLog(&model.AccountChangeLog{
				CustomerId:     customer.Id,
				Delta:          -order.TotalPrice,
				Account:        customer.AccountBallance,
				Type:           2, // order.send
				RelatedOrderTN: order.TrackNumber,
				Reason:         "",
			})

		}
		fmt.Println(">>>>>>>>>>>>>>>>>>>> update all done......................")
	}
	return nil
}