Horje
fiffo in javascript Code Example
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);




Javascript

Related
set ByteArrayContent content type json Code Example set ByteArrayContent content type json Code Example
string array to int array javascript Code Example string array to int array javascript Code Example
javascript long integer Code Example javascript long integer Code Example
javascript formdata include object Code Example javascript formdata include object Code Example
how to access variables in a different script file Code Example how to access variables in a different script file Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8