mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
2612-есть ок OCR, нужно допиливать бота под новый flow для операторов
This commit is contained in:
@@ -78,8 +78,8 @@ func (s *Service) checkWriteAccess(userID, serverID uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProcessReceiptImage - Доступно всем (включая Операторов)
|
||||
func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, imgData []byte) (*drafts.DraftInvoice, error) {
|
||||
// ProcessDocument - Доступно всем (включая Операторов)
|
||||
func (s *Service) ProcessDocument(ctx context.Context, userID uuid.UUID, imgData []byte, filename string) (*drafts.DraftInvoice, error) {
|
||||
server, err := s.accountRepo.GetActiveServer(userID)
|
||||
if err != nil || server == nil {
|
||||
return nil, fmt.Errorf("no active server for user")
|
||||
@@ -90,7 +90,7 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
photoID := uuid.New()
|
||||
draftID := uuid.New()
|
||||
|
||||
fileName := fmt.Sprintf("receipt_%s.jpg", photoID.String())
|
||||
fileName := fmt.Sprintf("receipt_%s_%s", photoID.String(), filename)
|
||||
filePath := filepath.Join(s.storagePath, serverID.String(), fileName)
|
||||
|
||||
// 2. Создаем директорию если не существует
|
||||
@@ -102,7 +102,7 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
if err := os.WriteFile(filePath, imgData, 0644); err != nil {
|
||||
return nil, fmt.Errorf("failed to save image: %w", err)
|
||||
}
|
||||
fileURL := "/uploads/" + fileName
|
||||
fileURL := fmt.Sprintf("/uploads/%s/%s", serverID.String(), fileName)
|
||||
|
||||
// 4. Создаем запись ReceiptPhoto
|
||||
photo := &photos.ReceiptPhoto{
|
||||
@@ -111,7 +111,7 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
UploadedBy: userID,
|
||||
FilePath: filePath,
|
||||
FileURL: fileURL,
|
||||
FileName: fileName,
|
||||
FileName: filename,
|
||||
FileSize: int64(len(imgData)),
|
||||
DraftID: &draftID, // Сразу связываем с будущим черновиком
|
||||
CreatedAt: time.Now(),
|
||||
@@ -140,14 +140,26 @@ func (s *Service) ProcessReceiptImage(ctx context.Context, userID uuid.UUID, img
|
||||
}
|
||||
|
||||
// 6. Отправляем в Python OCR
|
||||
rawResult, err := s.pyClient.ProcessImage(ctx, imgData, "receipt.jpg")
|
||||
rawResult, err := s.pyClient.ProcessImage(ctx, imgData, filename)
|
||||
if err != nil {
|
||||
draft.Status = drafts.StatusError
|
||||
_ = s.draftRepo.Update(draft)
|
||||
return nil, fmt.Errorf("python ocr error: %w", err)
|
||||
}
|
||||
|
||||
// 6. Матчинг и сохранение позиций
|
||||
// Парсим дату документа
|
||||
var dateIncoming *time.Time
|
||||
if rawResult.DocDate != "" {
|
||||
if t, err := time.Parse("02.01.2006", rawResult.DocDate); err == nil {
|
||||
dateIncoming = &t
|
||||
}
|
||||
}
|
||||
|
||||
// Заполняем номер и дату документа
|
||||
draft.IncomingDocumentNumber = rawResult.DocNumber
|
||||
draft.DateIncoming = dateIncoming
|
||||
|
||||
// 7. Матчинг и сохранение позиций
|
||||
var draftItems []drafts.DraftInvoiceItem
|
||||
for _, rawItem := range rawResult.Items {
|
||||
item := drafts.DraftInvoiceItem{
|
||||
|
||||
Reference in New Issue
Block a user