Documentation
¶
Index ¶
- func Migrate(db *gorm.DB) error
- type PurchaseService
- func (s *PurchaseService) AddItem(purchase *models.PurchaseOrderModel, data *models.PurchaseOrderItemModel) error
- func (s *PurchaseService) CalculateTaxes(baseAmount float64, isCompound bool, taxes []*models.TaxModel) (float64, float64, map[string]float64)
- func (s *PurchaseService) CancelPurchaseOrder(poID uint) error
- func (s *PurchaseService) ClearTransaction(id string) error
- func (s *PurchaseService) CreatePayment(poID string, date time.Time, amount float64, accountPayableID *string, ...) error
- func (s *PurchaseService) CreatePurchaseOrder(data *models.PurchaseOrderModel) error
- func (s *PurchaseService) CreatePurchasePayment(purchase *models.PurchaseOrderModel, ...) error
- func (s *PurchaseService) DeleteItem(purchase *models.PurchaseOrderModel, itemID string) error
- func (s *PurchaseService) DeletePurchase(id string) error
- func (s *PurchaseService) GetBalance(purchase *models.PurchaseOrderModel) (float64, error)
- func (s *PurchaseService) GetItems(id string) ([]models.PurchaseOrderItemModel, error)
- func (s *PurchaseService) GetPurchaseByID(id string) (*models.PurchaseOrderModel, error)
- func (s *PurchaseService) GetPurchases(request http.Request, search string) (paginate.Page, error)
- func (s *PurchaseService) PostPurchase(id string, data *models.PurchaseOrderModel, userID string, date time.Time) error
- func (s *PurchaseService) ReceivePurchaseOrder(date time.Time, poID, warehouseID string, description string) error
- func (s *PurchaseService) UpdateItem(purchase *models.PurchaseOrderModel, itemID string, ...) error
- func (s *PurchaseService) UpdatePurchase(id string, data *models.PurchaseOrderModel) error
- func (s *PurchaseService) UpdateTotal(purchase *models.PurchaseOrderModel) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Migrate ¶
Migrate migrates the purchase database model to the given database connection.
It uses gorm's AutoMigrate method to create the tables if they don't exist, and to migrate the existing tables if they do.
AutoMigrate will add missing columns, but won't change existing column's type or delete unused column, it also won't delete/rename tables.
Types ¶
type PurchaseService ¶
type PurchaseService struct {
// contains filtered or unexported fields
}
func NewPurchaseService ¶
func NewPurchaseService(db *gorm.DB, ctx *context.ERPContext, financeService *finance.FinanceService, stockMovementService *stockmovement.StockMovementService) *PurchaseService
NewPurchaseService creates a new instance of PurchaseService with the given database connection, context, finance service and stock movement service.
func (*PurchaseService) AddItem ¶
func (s *PurchaseService) AddItem(purchase *models.PurchaseOrderModel, data *models.PurchaseOrderItemModel) error
AddItem adds a new item to the purchase order with the given ID.
It takes a pointer to a PurchaseOrderModel which contains the purchase order details and a pointer to a PurchaseOrderItemModel which contains the item details. The function creates a new record in the purchase order items table with the given data. It also updates the Total and Paid fields of the purchase order. The function returns an error if the operation fails.
func (*PurchaseService) CalculateTaxes ¶
func (s *PurchaseService) CalculateTaxes(baseAmount float64, isCompound bool, taxes []*models.TaxModel) (float64, float64, map[string]float64)
CalculateTaxes calculates the total tax for a given base amount and a list of tax models.
If the isCompound flag is true, the total tax is calculated by adding the tax amount of each tax model to the total amount. If the isCompound flag is false, the total tax is calculated by adding the total tax amount of all tax models to the total amount. The function returns the total amount after tax, the total tax amount, and a map of tax name to tax amount.
func (*PurchaseService) CancelPurchaseOrder ¶
func (s *PurchaseService) CancelPurchaseOrder(poID uint) error
CancelPurchaseOrder cancels a purchase order with the given ID.
This function retrieves the purchase order from the database and checks if its status is "pending". If the purchase order is already processed, it returns an error. Otherwise, it updates the status to "cancelled". Returns an error if the purchase order retrieval or update operation fails.
Params: - poID (uint): The ID of the purchase order to be cancelled.
Returns: - (error): An error object if the cancellation fails or if the purchase order is already processed.
func (*PurchaseService) ClearTransaction ¶
func (s *PurchaseService) ClearTransaction(id string) error
ClearTransaction clears all transaction with given id as reference id.
This function runs inside a transaction, so if any error occurs, it will rollback.
It uses gorm's Delete method to delete all transaction with given id as reference id.
func (*PurchaseService) CreatePayment ¶
func (s *PurchaseService) CreatePayment(poID string, date time.Time, amount float64, accountPayableID *string, accountAssetID string) error
CreatePayment creates a new payment transaction associated with a purchase order.
The function takes as parameters:
- poID: the ID of the purchase order to be paid
- date: the date of the payment
- amount: the amount of the payment
- accountPayableID: the ID of the account payable associated with the purchase order
- accountAssetID: the ID of the asset account to be debited
It performs the following operations:
- Retrieves the purchase order from the database and checks if its status is "pending".
- Checks if the payment amount is greater than the total amount of the purchase order. If so, it returns an error.
- Creates a new transaction record in the database with the following details: - Date: the provided date - AccountID: the ID of the asset account to be debited - Description: "Pembayaran [purchase number]" - Notes: the description of the purchase order - TransactionRefID: the ID of the purchase order - TransactionRefType: "purchase" - CompanyID: the ID of the company associated with the purchase order - Debit: the payment amount
- If accountPayableID is not nil, it creates another transaction record with the following details: - AccountID: the ID of the account payable associated with the purchase order - Credit: the payment amount
- Updates the purchase order record in the database with the new paid amount.
- If the paid amount is equal to the total amount, it updates the status of the purchase order to "paid".
- Commits the transaction if all operations are successful. Otherwise, it rolls back the transaction.
Returns an error if any of the operations fail.
func (*PurchaseService) CreatePurchaseOrder ¶
func (s *PurchaseService) CreatePurchaseOrder(data *models.PurchaseOrderModel) error
CreatePurchaseOrder creates a new purchase order in the database.
It accepts a pointer to a PurchaseOrderModel which contains the purchase order details. The function retrieves the company ID from the request header and verifies the existence of an inventory account associated with the company. If the inventory account is not found, it returns an error. Otherwise, it saves the purchase order data in the database.
Returns an error if the creation of the purchase order fails or if the inventory account is not found.
func (*PurchaseService) CreatePurchasePayment ¶
func (s *PurchaseService) CreatePurchasePayment(purchase *models.PurchaseOrderModel, purchasePayment *models.PurchasePaymentModel) error
CreatePurchasePayment creates a new purchase payment transaction associated with a purchase order.
The function takes as parameters a pointer to a PurchaseOrderModel and a pointer to a PurchasePaymentModel which contains the payment details. It performs the following operations:
- Verifies if the payment amount is greater than the remaining balance of the purchase order. If so, it returns an error.
- Retrieves the payment account associated with the purchase order and verifies its type. If the account type is not LIABILITY, it returns an error.
- Creates a new transaction record in the database with the following details: - Date: the provided date - AccountID: the ID of the liability account associated with the purchase order - Description: "Pembayaran [purchase number]" - Notes: the description of the purchase order - TransactionRefID: the ID of the asset account associated with the payment - TransactionRefType: "transaction" - CompanyID: the ID of the company associated with the purchase order - Debit: the payment amount
- Creates another transaction record with the following details: - AccountID: the ID of the asset account associated with the payment - Credit: the payment amount
- If the payment discount is greater than 0, it creates another transaction record with the following details: - AccountID: the ID of the inventory account associated with the company - Credit: the discount amount
- Saves the purchase payment data in the database.
- Commits the transaction if all operations are successful. Otherwise, it rolls back the transaction.
Returns an error if any of the operations fail.
func (*PurchaseService) DeleteItem ¶
func (s *PurchaseService) DeleteItem(purchase *models.PurchaseOrderModel, itemID string) error
DeleteItem deletes a purchase order item from the database by its ID and updates the total cost of the associated purchase order.
The function takes a pointer to a PurchaseOrderModel and an item ID as a string. It deletes the item associated with the given ID from the database and updates the total cost of the purchase order by calling UpdateTotal. The function returns an error if the deletion or update operation fails.
func (*PurchaseService) DeletePurchase ¶
func (s *PurchaseService) DeletePurchase(id string) error
func (*PurchaseService) GetBalance ¶
func (s *PurchaseService) GetBalance(purchase *models.PurchaseOrderModel) (float64, error)
GetBalance calculates the remaining balance of a purchase order.
If the payment account is an asset account, it returns 0 immediately. Otherwise, it calculates the total payment amount made to the purchase order, and returns the difference between the purchase order total and the total payment. If the payment is more than the total, it returns an error.
func (*PurchaseService) GetItems ¶
func (s *PurchaseService) GetItems(id string) ([]models.PurchaseOrderItemModel, error)
func (*PurchaseService) GetPurchaseByID ¶
func (s *PurchaseService) GetPurchaseByID(id string) (*models.PurchaseOrderModel, error)
GetPurchaseByID retrieves a purchase order from the database by ID.
It takes a purchase order ID as input and returns a pointer to a PurchaseOrderModel containing the purchase order details. If the purchase order is a return, it also retrieves the original purchase order and stores it in the PurchaseRef field. The function calculates the total amount paid by iterating over the purchase payments and updating the Paid field of the purchase order. The function returns an error if the operation fails.
func (*PurchaseService) GetPurchases ¶
GetPurchases retrieves a paginated list of purchase orders from the database.
It takes an http.Request and a search query string as input. The method uses GORM to query the database for purchase orders, applying the search query to the purchase order description and purchase number fields. If the request contains a company ID header, the method also filters the result by the company ID. The function utilizes pagination to manage the result set and applies any necessary request modifications using the utils.FixRequest utility.
The function returns a paginated page of PurchaseOrderModel and an error if the operation fails.
func (*PurchaseService) PostPurchase ¶
func (s *PurchaseService) PostPurchase(id string, data *models.PurchaseOrderModel, userID string, date time.Time) error
PostPurchase posts a purchase order with the given ID and data, and updates the status of the purchase order to "POSTED".
The function takes a pointer to a PurchaseOrderModel and a string representing the user ID. It updates the status of the purchase order to "POSTED", and sets the published at and published by fields. It then creates a new transaction for each item in the purchase order, and updates the total cost of the purchase order. The function returns an error if any of the operations fail.
func (*PurchaseService) ReceivePurchaseOrder ¶
func (s *PurchaseService) ReceivePurchaseOrder(date time.Time, poID, warehouseID string, description string) error
ReceivePurchaseOrder processes the receipt of a purchase order into a specified warehouse.
It accepts the date of receipt, the purchase order ID, the warehouse ID, and a description of the transaction. The function checks if the purchase order is in a "pending" state and, if so, creates stock movements for each item in the purchase order, updating the stock status to "received". It performs these operations within a transaction to ensure data consistency. Returns an error if the purchase order is already processed or if any database operations fail.
func (*PurchaseService) UpdateItem ¶
func (s *PurchaseService) UpdateItem(purchase *models.PurchaseOrderModel, itemID string, item *models.PurchaseOrderItemModel) error
func (*PurchaseService) UpdatePurchase ¶
func (s *PurchaseService) UpdatePurchase(id string, data *models.PurchaseOrderModel) error
UpdatePurchase updates the purchase order with the given id with the given data.
It takes the id of the purchase order to be updated and a pointer to a PurchaseOrderModel which contains the updated data of the purchase order. The function returns an error if the update operation fails.
func (*PurchaseService) UpdateTotal ¶
func (s *PurchaseService) UpdateTotal(purchase *models.PurchaseOrderModel) error