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 }
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 }