new api for offline sales
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"nearle/models"
|
||||
"net/http"
|
||||
@@ -371,6 +372,72 @@ func (ctl *OrderController) CreateOrderv3(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// UploadOfflineSales imports a spreadsheet of in-store counter sales.
|
||||
//
|
||||
// The response is 200 whenever the batch was processed, even if individual
|
||||
// bills were rejected, because a partial import is a normal outcome for a
|
||||
// spreadsheet and the per-bill results carry the detail. A non-200 means
|
||||
// nothing at all was attempted — a malformed body, or an outlet the caller has
|
||||
// no claim on.
|
||||
func (ctl *OrderController) UploadOfflineSales(c *fiber.Ctx) error {
|
||||
var input models.OfflineSalesUpload
|
||||
|
||||
if err := c.BodyParser(&input); err != nil {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"message": "could not read the upload: " + err.Error(),
|
||||
"status": false,
|
||||
})
|
||||
}
|
||||
|
||||
if input.Tenantid <= 0 || input.Locationid <= 0 {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"message": "tenantid and locationid are required",
|
||||
"status": false,
|
||||
})
|
||||
}
|
||||
|
||||
if len(input.Bills) == 0 {
|
||||
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||
"code": http.StatusBadRequest,
|
||||
"message": "no sales rows found in the upload",
|
||||
"status": false,
|
||||
})
|
||||
}
|
||||
|
||||
result, err := ctl.orderService.UploadOfflineSales(input)
|
||||
if err != nil {
|
||||
log.Println("UploadOfflineSales service error:", err)
|
||||
// An outlet the caller doesn't own is a permission problem, not a
|
||||
// server fault, and is reported as one so the UI can say so plainly.
|
||||
statusCode := http.StatusInternalServerError
|
||||
if strings.Contains(strings.ToLower(err.Error()), "does not belong to tenant") {
|
||||
statusCode = http.StatusForbidden
|
||||
}
|
||||
return c.Status(statusCode).JSON(fiber.Map{
|
||||
"code": statusCode,
|
||||
"message": err.Error(),
|
||||
"status": false,
|
||||
})
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("%d bill(s) imported", result.Imported)
|
||||
if result.Duplicate > 0 {
|
||||
message += fmt.Sprintf(", %d already imported", result.Duplicate)
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
message += fmt.Sprintf(", %d failed", result.Failed)
|
||||
}
|
||||
|
||||
return c.Status(http.StatusOK).JSON(fiber.Map{
|
||||
"code": http.StatusOK,
|
||||
"message": message,
|
||||
"status": true,
|
||||
"details": result,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctl *OrderController) GetCustomerOrders(c *fiber.Ctx) error {
|
||||
customerID := c.Query("customerid")
|
||||
tenantID := c.Query("tenantid")
|
||||
|
||||
@@ -309,6 +309,40 @@ func (ctl *ProductController) GetLocationProducts(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// GetSaleTemplate serves the data the web app turns into the offline-sales
|
||||
// spreadsheet. Both tenantid and locationid are required rather than defaulted:
|
||||
// a template generated against the wrong outlet would carry productids the
|
||||
// import then rejects, which is a confusing failure a missing parameter should
|
||||
// not be able to cause.
|
||||
func (ctl *ProductController) GetSaleTemplate(c *fiber.Ctx) error {
|
||||
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
|
||||
locationID, _ := strconv.Atoi(c.Query("locationid"))
|
||||
|
||||
if tenantID <= 0 || locationID <= 0 {
|
||||
return c.JSON(fiber.Map{
|
||||
"status": false,
|
||||
"code": http.StatusBadRequest,
|
||||
"message": "tenantid and locationid are required",
|
||||
})
|
||||
}
|
||||
|
||||
result, err := ctl.productService.GetSaleTemplate(tenantID, locationID)
|
||||
if err != nil {
|
||||
return c.JSON(fiber.Map{
|
||||
"status": false,
|
||||
"code": http.StatusInternalServerError,
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"status": true,
|
||||
"code": http.StatusOK,
|
||||
"message": "Success",
|
||||
"details": result,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctl *ProductController) GetLocationProductSummary(c *fiber.Ctx) error {
|
||||
tenantID, _ := strconv.Atoi(c.Query("tenantid"))
|
||||
locationID, _ := strconv.Atoi(c.Query("locationid"))
|
||||
|
||||
Reference in New Issue
Block a user