/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package erp.mtrn.data;

import java.util.ArrayList;

/**
 *
 * @author Irving Sánchez
 */
public class SGuiDpsEntryPrice {
    private double mdQtyUsedOther;
    private double mdQtyUsedOwn;
    
    private SDataDpsEntryPrice moDataDpsSourceEntryPrice;
    private ArrayList<SDataDpsEntry> maDataDpsDestinyEntries;
    
    public SGuiDpsEntryPrice(SDataDpsEntryPrice dataDpsSourceEntryPrice) {
        moDataDpsSourceEntryPrice = dataDpsSourceEntryPrice;
        maDataDpsDestinyEntries = new ArrayList<>();
    }

    public void setQtyUsedOther(double qty) { mdQtyUsedOther = qty; }
    public void setQtyUsedOwn(double qty) { mdQtyUsedOwn = qty; }
    public double getQtyUsedOther() { return mdQtyUsedOther; }
    public double getQtyUsedOwn() { return mdQtyUsedOwn; }

    public SDataDpsEntryPrice getDataDpsEntryPrice() { return moDataDpsSourceEntryPrice; }
    public ArrayList<SDataDpsEntry> getDataDpsDestinyEntries() { return maDataDpsDestinyEntries; }

    public double obtainQtyAvailable() {
        updateQtyOwn();
        return moDataDpsSourceEntryPrice.getOriginalQuantity() - mdQtyUsedOther - mdQtyUsedOwn;
    }
    
    public double obtainQtyAvailable(SDataDpsEntry dataDpsDestinyEntry) {
        updateQtyOwn();
        return obtainQtyAvailable() + dataDpsDestinyEntry.getOriginalQuantity();
    }

    public void updateQtyOwn() {
        mdQtyUsedOwn = 0;
        for (SDataDpsEntry dpsDestinyEntry : maDataDpsDestinyEntries) {
            mdQtyUsedOwn += dpsDestinyEntry.getOriginalQuantity();
        }
    }
    
    public void addDataDpsDestinyEntry(SDataDpsEntry dataDpsDestinyEntry) {
        maDataDpsDestinyEntries.add(dataDpsDestinyEntry);
        updateQtyOwn();
    }
    
    public void removeDataDpsDestinyEntry(SDataDpsEntry dataDpsDestinyEntry) {
        maDataDpsDestinyEntries.remove(dataDpsDestinyEntry);
        updateQtyOwn();
    }
    
    public void updateDataDpsDestinyEntry(SDataDpsEntry oldDataDpsDestinyEntry, SDataDpsEntry newDataDpsDestinyEntry) {
        removeDataDpsDestinyEntry(oldDataDpsDestinyEntry);
        addDataDpsDestinyEntry(newDataDpsDestinyEntry);
    
    }
}

@@ -229,16 +229,41 @@
 rayList;
+%0Aimport sa.lib.SLibUtils;
 %0A%0A/**%0A *
@@ -288,16 +288,30 @@
  S%C3%A1nchez
+, Daniel L%C3%B3pez
 %0A */%0Apub
@@ -1256,32 +1256,48 @@
 %0A        return 
+SLibUtils.round(
 moDataDpsSourceE
@@ -1355,24 +1355,95 @@
 mdQtyUsedOwn
+, SLibUtils.getDecimalFormatAmountUnitary().getMaximumFractionDigits())
 ;%0A    %7D%0A    
@@ -1556,16 +1556,32 @@
  return 
+SLibUtils.round(
 obtainQt
@@ -1632,24 +1632,95 @@
 alQuantity()
+, SLibUtils.getDecimalFormatAmountUnitary().getMaximumFractionDigits())
 ;%0A    %7D%0A%0A   

Fix to rounding issue in estimating available quantity.