Changeset 597


Ignore:
Timestamp:
Jun 15, 2010, 9:18:21 AM (10 years ago)
Author:
gav
Message:

Improvements to purchasing, update order amount with total invoice payment amounts and set flags.

Location:
trunk/grails-app
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/InventoryItemPurchaseDetailedController.groovy

    r596 r597  
    320320
    321321    def approveInvoicePayment = {
     322        // Read the values from Order Received.
    322323        def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id )
    323324
     
    331332        params.returnTo = params.returnTo ?: 'inventoryItem'
    332333        return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance,
    333                         'orderId': inventoryItemPurchaseInstance.id]
     334                        'receivedId': inventoryItemPurchaseInstance.id]
    334335    }
    335336
     
    359360        render(view:'approveInvoicePayment',
    360361                    model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance,
    361                                     'orderId': result.orderId])
     362                                    'receivedId': result.receivedId])
    362363    }
    363364}
  • trunk/grails-app/services/CreateDataService.groovy

    r587 r597  
    637637                                                                                description: "Order has been partially received.")
    638638        saveAndTest(inventoryItemPurchaseTypeInstance)
     639
    639640        // InventoryItemPurchaseType #3
    640641        inventoryItemPurchaseTypeInstance = new InventoryItemPurchaseType(name: "Received Complete",
  • trunk/grails-app/services/InventoryPurchaseService.groovy

    r596 r597  
    1111    * @param order An inventory puchase that was the source of the order.
    1212    * @returns A result map containing the totalOrdered, totalReceived, totalRemaining, thisOrderRemaining,
    13     *                 totalOrderedAmount, totalReceivedAmount, totalRemainingAmount, thisOrderRemainingAmount.
     13    *                 totalOrderedAmount, totalReceivedAmount, totalRemainingAmount, thisOrderRemainingAmount,
     14    *                 totalPaymentApproved.
    1415    */
    1516    def calcQuantities(order) {
     
    1920        result.totalOrdered = 0
    2021        result.totalOrderedAmount = 0
     22        result.totalReceived = 0
     23        result.totalReceivedAmount = 0
     24        result.totalPaymentApproved = 0
    2125        InventoryItemPurchase.withCriteria {
    2226            eq("inventoryItem", order.inventoryItem)
    2327            eq("purchaseOrderNumber", order.purchaseOrderNumber)
    24             inventoryItemPurchaseType {
    25                     eq("id", 1L)
    26             }
    2728        }.each() {
    28             result.totalOrdered += it.quantity
    29             result.totalOrderedAmount += it.orderValueAmount
    30         }
    31 
    32         result.totalReceived = 0
    33         result.totalReceivedAmount = 0
    34         InventoryItemPurchase.withCriteria {
    35             eq("inventoryItem", order.inventoryItem)
    36             eq("purchaseOrderNumber", order.purchaseOrderNumber)
    37             inventoryItemPurchaseType {
    38                 or {
    39                     eq("id", 2L)
    40                     eq("id", 3L)
    41                 }
    42             }
    43         }.each() {
    44             result.totalReceived += it.quantity
    45             result.totalReceivedAmount += it.orderValueAmount
     29            if(it.inventoryItemPurchaseType.id == 1L) { // Orders.
     30                result.totalOrdered += it.quantity
     31                result.totalOrderedAmount += it.orderValueAmount
     32            }
     33            if(it.inventoryItemPurchaseType.id == 2L || it.inventoryItemPurchaseType.id == 3L) { // Received B/order and Complete.
     34                result.totalReceived += it.quantity
     35                result.totalReceivedAmount += it.orderValueAmount
     36            }
     37            if(it.inventoryItemPurchaseType.id == 4L) { // Approved.
     38                result.totalPaymentApproved += it.orderValueAmount
     39            }
    4640        }
    4741
     
    7165
    7266        return result
     67    }
     68
     69    /**
     70    * Get the original order for an inventoryItemPurchase and InventoryItem.
     71    * @param inventoryItemPurchase An inventory puchase.
     72    * @returns The order.
     73    */
     74    def getOriginalOrder(inventoryItemPurchase) {
     75
     76        def namedParams = [:]
     77
     78        namedParams.inventoryItem = inventoryItemPurchase.inventoryItem
     79        namedParams.purchaseOrderNumber = inventoryItemPurchase.purchaseOrderNumber
     80        namedParams.orderPlaced = InventoryItemPurchaseType.read(1)
     81
     82        def order = InventoryItemPurchase.find("from InventoryItemPurchase as p \
     83                                                                                where( p.inventoryItem = :inventoryItem \
     84                                                                                            and p.purchaseOrderNumber = :purchaseOrderNumber \
     85                                                                                            and p.inventoryItemPurchaseType = :orderPlaced )",
     86                                                                            namedParams)
     87
     88        return order
    7389    }
    7490
     
    93109
    94110            // Handle Invoice Payment Approved.
    95             // Find and mark all orders as invoicePaymentApproved = false.
    96111            if(purchaseTypeId == 4) {
     112                // Find and mark all orders as invoicePaymentApproved = false.
    97113                InventoryItemPurchase.withCriteria {
    98114                    eq("inventoryItem", result.inventoryItemPurchaseInstance.inventoryItem)
     
    104120                    it.invoicePaymentApproved = false
    105121                }
     122                // Find and mark last orderReceived as invoicePaymentApproved = false.
     123                InventoryItemPurchase.withCriteria {
     124                    eq("inventoryItem", result.inventoryItemPurchaseInstance.inventoryItem)
     125                    eq("purchaseOrderNumber", result.inventoryItemPurchaseInstance.purchaseOrderNumber)
     126                    inventoryItemPurchaseType {
     127                        or {
     128                            eq("id", 2L) // Received B/order To Come
     129                            eq("id", 3L) // Received Complete
     130                        }
     131                    }
     132                }.last().invoicePaymentApproved = false
    106133            }
    107134
     
    129156                    between("date", startOfDay, startOfDay+1)
    130157                    inventoryMovementType {
    131                         eq("id", 3L) //purchaseReceived.
     158                        eq("id", 3L) // purchaseReceived.
    132159                    }
    133160                    order('id', 'desc') // The newest one will be first.
     
    324351            }
    325352
    326             def order = InventoryItemPurchase.get(params.orderId)
     353            def received = InventoryItemPurchase.get(params.receivedId)
     354            if(!received)
     355                return fail(code:"default.not.found")
     356            result.receivedId = received.id
     357
     358            def order = getOriginalOrder(received)
    327359            if(!order)
    328360                return fail(code:"default.not.found")
    329361            result.orderId = order.id
    330 
    331             result.inventoryItemPurchaseInstance = new InventoryItemPurchase(params)
    332362
    333363            result.inventoryItemPurchaseInstance = new InventoryItemPurchase(params)
     
    338368            result.inventoryItemPurchaseInstance.inventoryItemPurchaseType = InventoryItemPurchaseType.read(4) // Approve.
    339369
    340             order.invoicePaymentApproved = true
     370            received.invoicePaymentApproved = true
    341371            result.inventoryItemPurchaseInstance.invoicePaymentApproved = true
     372
     373            // Update orderValueAmount and invoicePaymentApproved if processing a receivedComplete.
     374            if(received.inventoryItemPurchaseType.id == 3L) {
     375                order.invoicePaymentApproved = true
     376                result.inventoryItemPurchaseInstance.receivedComplete = true
     377                def calcQuantities = calcQuantities(order)
     378                if(result.inventoryItemPurchaseInstance.orderValueAmount)
     379                    calcQuantities.totalPaymentApproved += result.inventoryItemPurchaseInstance.orderValueAmount
     380                order.orderValueAmount = calcQuantities.totalPaymentApproved
     381            }
    342382
    343383            // Fetch to prevent lazy initialization error.
     
    347387                return fail(field:"invoiceNumber", code:"inventoryItemPurchase.invoiceNumber.required")
    348388
    349             order.invoicePaymentApproved = true
    350 
    351389            if(order.hasErrors() || !order.save())
    352390                return fail(code:"default.create.failure")
  • trunk/grails-app/views/inventoryItemPurchaseDetailed/approveInvoicePayment.gsp

    r595 r597  
    2020            <g:form action="approveInvoicePaymentSave" method="post" >
    2121                <g:hiddenField name="inventoryItem.id" value="${inventoryItemPurchaseInstance.inventoryItem?.id}" />
    22                 <g:hiddenField name="orderId" value="${orderId}" />
     22                <g:hiddenField name="receivedId" value="${receivedId}" />
    2323                <g:hiddenField name="returnTo" value="${params.returnTo}" />
    2424                <div class="dialog">
Note: See TracChangeset for help on using the changeset viewer.