// update one's account ballance; func (s *AccountService) UpdateAccountBalance(personId int, delta float64, reason string, relatedOrderTrackingNo int64) { if person, err := Person.GetPersonById(personId); err != nil { panic(err) } else if person == nil { panic(fmt.Sprintf("Person %d not found!", personId)) } else { person.AccountBallance += delta // p.Order.SumOrderPrice() _, err := personservice.Update(person) // TODO if err != nil { panic(err.Error()) } // create chagne log at the same time: accountdao.CreateAccountChangeLog(&model.AccountChangeLog{ CustomerId: person.Id, Delta: delta, // -p.Order.SumOrderPrice(), Account: person.AccountBallance, // Type: 2, // create takeaway order RelatedOrderTN: relatedOrderTrackingNo, // p.Order.TrackNumber, Reason: reason, }) } }
func (s *OrderService) BatchCloseOrder(money float64, customerId int) { debug.Log("Incoming Money: %v", money) person, err := Person.GetPersonById(customerId) if err != nil { panic(err.Error()) } // get unclosed orders for somebody orders, err := orderdao.DeliveringUnclosedOrdersByCustomer(customerId) if err != nil { panic(err.Error()) } // collect totalorder price var totalOrderPrice float64 for _, o := range orders { totalOrderPrice += o.SumOrderPrice() } // money used as total shouldbe: inputmoney + (accountballance - allorder's price) totalmoney := money + (person.AccountBallance + totalOrderPrice) for _, order := range orders { if totalmoney-order.SumOrderPrice() >= 0 { err := s.ChangeOrderStatus(order.TrackNumber, "done") if err != nil { panic(err.Error()) } totalmoney -= order.SumOrderPrice() } } accountdao.CreateIncoming(&model.AccountIncoming{ CustomeId: person.Id, Incoming: money, }) // modify customer's accountballance person.AccountBallance += money personservice.Update(person) // TODO: chagne place // create chagne log at the same time: accountdao.CreateAccountChangeLog(&model.AccountChangeLog{ CustomerId: person.Id, Delta: money, Account: person.AccountBallance, Type: 2, // create order // RelatedOrderTN: 0, Reason: "Batch insert", }) }
// on form submit func (p *EditAccountBallance) OnSuccess() (string, string) { // init person again. get person. p.Setup() if p.AccountBallance != p.Person.AccountBallance { // create accountdao.CreateAccountChangeLog(&model.AccountChangeLog{ CustomerId: p.Person.Id, Delta: p.AccountBallance - p.Person.AccountBallance, Account: p.AccountBallance, Type: 1, // manually modification; RelatedOrderTN: 0, Reason: p.Reason, }) // update account ballance p.Person.AccountBallance = p.AccountBallance if _, err := personservice.Update(p.Person); err != nil { panic(err) } } return "redirect", fmt.Sprintf("/person/EditAccountBallance/%v", p.Id.Int) }
// **** 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") }
// **** 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 }
func (s *AccountService) CreateAccountChangeLog(acl *model.AccountChangeLog) ( *model.AccountChangeLog, error) { return accountdao.CreateAccountChangeLog(acl) }