Skip to content

Instantly share code, notes, and snippets.

@morkai
Last active January 20, 2019 04:04
Show Gist options
  • Save morkai/ec86101111e5b58fd078646509047801 to your computer and use it in GitHub Desktop.
Save morkai/ec86101111e5b58fd078646509047801 to your computer and use it in GitHub Desktop.
Dodaje pole Koszty uboczne brutto do formularza wydatku Faktura VAT ułatwiający dodanie kosztów transportu. https://youtu.be/GCNu5bHyVkQ
// ==UserScript==
// @name wfirma-koszty-uboczne-zakupu
// @namespace https://miracle.systems/
// @version 0.1
// @description Dodaje pole Koszty uboczne brutto do formularza wydatku Faktura VAT ułatwiający dodanie kosztów transportu.
// @author morkai
// @match https://wfirma.pl/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
setInterval(fixTransport, 1000);
function fixTransport()
{
$('form[action^="/expenses/add/invoice"]').each(function()
{
if (this.classList.contains('transport-fixed'))
{
return;
}
const $form = $(this).addClass('transport-fixed');
const $transportInput = $(`<input type="text" placeholder="Koszty uboczne brutto…" autocomplete="off" style="margin: 7px 0 0 50px; width: 140px">`);
$transportInput.on('keydown', function(e)
{
this.style.color = '';
if (e.key !== 'Enter')
{
return;
}
const value = parseFloat($transportInput.val().replace(',', '.').replace(/[^0-9.]/g, ''), 10);
if (isNaN(value) || value <= 0)
{
this.style.color = 'red';
return false;
}
const $accordion = $form.find('.items-accordion');
const $items = $form.find('.items-accordion').find('.ui-accordion-header:not(.static)');
if ($items.length === 0)
{
$accordion
.find('.ui-accordion-header')
.last()
.find('.expense-types')
.data('wfirma-Select')
.wrapper
.find('.dropdown-toggle')
.click();
return false;
}
let $totalItem = null;
let $transportItem = null;
$items.each(function()
{
const $item = $(this);
if (/koszty uboczne/i.test($item.find('.search-select').val()))
{
if (!$transportItem)
{
$transportItem = $item;
}
}
else if (!$totalItem)
{
$totalItem = $item;
}
});
if (!$totalItem.length)
{
return false;
}
const $twoThree = $totalItem.next().find('.row option[value="222"]:selected').first();
if (!$twoThree.length)
{
return false;
}
const $brutto = $twoThree.closest('.row').find('.input-brutto');
const oldBrutto = parseFloat($brutto.val().replace(',', '.').replace(/[^0-9.]/g, ''));
const newBrutto = oldBrutto - value;
if (newBrutto <= 0)
{
this.style.color = 'red';
return false;
}
$brutto.val(newBrutto.toFixed(2).replace('.', ',')).change();
if (!$transportItem)
{
createTransportItem();
}
else
{
setTransportItem();
}
$transportInput.val('');
return false;
function setTransportItem()
{
$transportItem
.next()
.find('.row option[value="222"]:selected')
.first()
.closest('.row')
.find('.input-brutto')
.val(value.toFixed(2).replace('.', ',')).change();
}
function createTransportItem()
{
const oldItemCount = $accordion.find('.ui-accordion-header').length;
$accordion
.find('.ui-accordion-header')
.last()
.find('a[data-text="Koszty uboczne zakupu"]')
.click();
waitForItem(0);
function waitForItem(i)
{
if (i === 40)
{
return;
}
const $items = $accordion.find('.ui-accordion-header');
if ($items.length <= oldItemCount)
{
return setTimeout(waitForItem, 50, i + 1);
}
$transportItem = $items.last().prev().prev();
setTransportItem();
}
}
});
$transportInput.insertAfter($form.find('.items-accordion'));
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment