Files
rmser/internal/infrastructure/repository/drafts/postgres.go
SERTY b4ce819931 добавил пользователей для сервера и роли
добавил инвайт-ссылки с ролью оператор для сервера
добавил супер-админку для смены владельцев
добавил уведомления о смене ролей на серверах
добавил модалку для фото прям в черновике
добавил UI для редактирования прав
2025-12-23 13:06:06 +03:00

110 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package drafts
import (
"rmser/internal/domain/drafts"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"gorm.io/gorm"
)
type pgRepository struct {
db *gorm.DB
}
func NewRepository(db *gorm.DB) drafts.Repository {
return &pgRepository{db: db}
}
func (r *pgRepository) Create(draft *drafts.DraftInvoice) error {
return r.db.Create(draft).Error
}
func (r *pgRepository) GetByID(id uuid.UUID) (*drafts.DraftInvoice, error) {
var draft drafts.DraftInvoice
err := r.db.
Preload("Items", func(db *gorm.DB) *gorm.DB {
return db.Order("draft_invoice_items.raw_name ASC")
}).
Preload("Items.Product").
Preload("Items.Product.MainUnit").
Preload("Items.Container").
Where("id = ?", id).
First(&draft).Error
if err != nil {
return nil, err
}
return &draft, nil
}
func (r *pgRepository) Update(draft *drafts.DraftInvoice) error {
return r.db.Model(draft).Updates(map[string]interface{}{
"status": draft.Status,
"document_number": draft.DocumentNumber,
"date_incoming": draft.DateIncoming,
"supplier_id": draft.SupplierID,
"store_id": draft.StoreID,
"comment": draft.Comment,
"rms_invoice_id": draft.RMSInvoiceID,
"rms_server_id": draft.RMSServerID,
"updated_at": gorm.Expr("NOW()"),
}).Error
}
func (r *pgRepository) CreateItems(items []drafts.DraftInvoiceItem) error {
if len(items) == 0 {
return nil
}
return r.db.CreateInBatches(items, 100).Error
}
func (r *pgRepository) CreateItem(item *drafts.DraftInvoiceItem) error {
return r.db.Create(item).Error
}
func (r *pgRepository) DeleteItem(itemID uuid.UUID) error {
return r.db.Delete(&drafts.DraftInvoiceItem{}, itemID).Error
}
func (r *pgRepository) UpdateItem(itemID uuid.UUID, productID *uuid.UUID, containerID *uuid.UUID, qty, price decimal.Decimal) error {
sum := qty.Mul(price)
isMatched := productID != nil
return r.db.Model(&drafts.DraftInvoiceItem{}).
Where("id = ?", itemID).
Updates(map[string]interface{}{
"product_id": productID,
"container_id": containerID,
"quantity": qty,
"price": price,
"sum": sum,
"is_matched": isMatched,
}).Error
}
func (r *pgRepository) Delete(id uuid.UUID) error {
return r.db.Delete(&drafts.DraftInvoice{}, id).Error
}
// GetActive возвращает черновики для конкретного СЕРВЕРА
func (r *pgRepository) GetActive(serverID uuid.UUID) ([]drafts.DraftInvoice, error) {
var list []drafts.DraftInvoice
activeStatuses := []string{
drafts.StatusProcessing,
drafts.StatusReadyToVerify,
drafts.StatusError,
drafts.StatusCanceled,
}
err := r.db.
Preload("Items").
Preload("Store").
Where("rms_server_id = ? AND status IN ?", serverID, activeStatuses). // Фильтр по серверу
Order("created_at DESC").
Find(&list).Error
return list, err
}