         shipping_address: { model: "Address" }
         sales_order:      { model: "SalesOrder",  readOnly:true }
         customer:         { model: "Customer",    readOnly:true }
-        location:         { model: "Location",    readOnly:true }
+        location:         { model: "Location",    readOnly:true, default: ->
+            Skr.Models.Location.fetchById(@location_id) if @location_id
+        }
         terms:            { model: "PaymentTerm", readOnly:true }
         pick_ticket:      { model: "PickTicket",  readOnly:true }
         lines:            { collection: "InvLine", inverse: 'invoice'  }

        id:                 {type:"integer", required:true}
        visible_id:         {type:"integer", required:true}
        terms_id:           {type:"integer", required:true}
        customer_id:        {type:"integer", required:true}
        location_id:        {type:"integer", required:true}
        sales_order_id:     "integer"
        pick_ticket_id:     "integer"


    constructor: ->
        super
        @lines.on('change:total', @onChangeTotal, this)
        @on('change:customer', @onSetCustomer)

    onChangeTotal: ->
        @trigger('change', @, {})
        @unCacheDerived('total')
        @unset('invoice_total')

    onSetCustomer: (newCustomer) ->
        return if not newCustomer or newCustomer.isNew()
        @copyAssociationsFrom( newCustomer, 'billing_address', 'shipping_address')

    setFromSalesOrder: (so) ->
        @sales_order_id = so.id
