Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fiffo in javascript

FIFO in Javascrip
<script>
    var purchase = [{
        "qty": 100,
        "price": 200,
        "amt": 0
      },
      {
        "qty": 275,
        "price": 210,
        "amt": 0
      },
      {
        "qty": 300,
        "price": 225,
        "amt": 0
      },
      {
        "qty": 500,
        "price": 275,
        "amt": 0
      },
    ];
    var sells = [{
        "qty": 600,
        "amt": 0,
        "purAmt": 0,
        "price": 300
      },
      {
        "qty": 275,
        "amt": 0,
        "purAmt": 0,
        "price": 350
      },
      {
        "qty": 300,
        "amt": 0,
        "purAmt": 0,
        "price": 400
      }
    ];
    purchase.forEach((element, key) => {
      purchase[key]['amt'] = element['qty'] * element['price'];
    });
    sells.forEach((sell, sell_key) => {
      var purAmount = 0;
      var balance = sell['qty'];
      var usedQty = 0;
      var detail = [];
      purchase.forEach((pur, pur_key) => {
        if (balance != 0) {
          if (pur['qty'] <= balance) {
            purAmount += pur['qty'] * pur['price'];
            detail.push(pur['qty'] + "*" + pur['price'] + "=" + pur['qty'] * pur['price'])
            balance -= pur.qty;
            purchase[pur_key]['qty'] = 0;
            delete purchase[pur_key];

          } else {
            if (pur['qty'] > balance) {
              balance -= pur['qty'];
              usedQty = pur['qty'] - Math.abs(balance);
              purchase[pur_key]['qty'] = Math.abs(balance);
              if (balance != 0) {
                purAmount += usedQty * pur['price'];
                detail.push(usedQty + "*" + pur['price'] + "=" + usedQty * pur['price']);
              }
              balance = Math.max(0, balance);
              return false;
            }
          }
        }
      });
      sells[sell_key]['details'] = [...detail];
      sells[sell_key]['purAmt'] = purAmount;
      sells[sell_key]['amt'] = sells[sell_key]['qty'] * sells[sell_key]['price'];
      sells[sell_key]['gainLoss'] = sells[sell_key]['amt'] - sells[sell_key]['purAmt'];
    });
    console.log(sells);
    console.log(purchase);
Comment

PREVIOUS NEXT
Code Example
Javascript :: express pass data between middleware 
Javascript :: get index from for in loop javascript 
Javascript :: material ui paper color default background 
Javascript :: why typescript is superset of javascript 
Javascript :: will stop the loop if the array has any negative number and return all the positive numbers before the negative numbers 
Javascript :: javascript grow function 
Javascript :: ES2022 - Top-level await modules 
Javascript :: AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal: 
Javascript :: how do i make http post in nodejs without third party 
Javascript :: disabling first item in dropdownlist 
Javascript :: typeorm class validation 
Javascript :: automatically adjust color 
Javascript :: how to get length of number in javascript 
Javascript :: material ui refresh icon 
Javascript :: Arrow functions by Codeacademy 
Javascript :: build an javascript URL and its search parameters 
Javascript :: Adding Notices in the Block Editor Wordpress 
Javascript :: ngFor fake 
Javascript :: js regexp eth wallet 
Javascript :: multiple counter for loop in javascript 
Javascript :: get src vanilla js 
Javascript :: js plugin for drop down with images 
Javascript :: fs keep file open 
Javascript :: filtrer un tableau javascript 
Javascript :: react native image path in vriable 
Javascript :: react load after scrolling 
Javascript :: remove image Input of element 
Javascript :: javascript to prevent method POST from realoading 
Javascript :: javascript chessboard embedding 
Javascript :: gsheet function argument a1notation 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =