From 81ed97c9ba935bf49f9357341c3b66c7576554df Mon Sep 17 00:00:00 2001
From: javierybar <36415318+javierybar@users.noreply.github.com>
Date: Sun, 25 Oct 2020 20:03:25 +0100
Subject: [PATCH 1/3] NEW Credit invoice in TakePOS
---
htdocs/takepos/index.php | 26 ++++--
htdocs/takepos/invoice.php | 157 +++++++++++++++++++++++++++++++++++++
2 files changed, 177 insertions(+), 6 deletions(-)
diff --git a/htdocs/takepos/index.php b/htdocs/takepos/index.php
index 8e6eb397c55..83a8fc7931b 100644
--- a/htdocs/takepos/index.php
+++ b/htdocs/takepos/index.php
@@ -25,13 +25,13 @@
//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
-//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
-//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
-if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
+//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
+//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
+if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
-if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
-if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
-if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
+if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
+if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
+if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
require '../main.inc.php'; // Load $user and permissions
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
@@ -888,6 +888,20 @@ if (empty($conf->global->TAKEPOS_HIDE_HEAD_BAR)) {
+
+
+
+
+
+
+
+
+
+
+
diff --git a/htdocs/takepos/invoice.php b/htdocs/takepos/invoice.php
index 3a67c81d4ba..00a227e8041 100644
--- a/htdocs/takepos/invoice.php
+++ b/htdocs/takepos/invoice.php
@@ -271,6 +271,154 @@ if ($action == 'valid' && $user->rights->facture->creer)
}
}
+if ($action == 'creditnote')
+{
+ $creditnote = new Facture($db);
+ $creditnote->socid = $invoice->socid;
+ $creditnote->date = dol_now();
+ $creditnote->type = Facture::TYPE_CREDIT_NOTE;
+ $creditnote->fk_facture_source = $placeid;
+ $creditnote->remise_absolue = $invoice->remise_absolue;
+ $creditnote->remise_percent = $invoice->remise_percent;
+ $creditnote->create($user);
+
+ foreach ($invoice->lines as $line)
+ {
+ // Extrafields
+ if (method_exists($line, 'fetch_optionals')) {
+ // load extrafields
+ $line->fetch_optionals();
+ }
+ // Reset fk_parent_line for no child products and special product
+ if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
+ $fk_parent_line = 0;
+ }
+ if ($invoice->type == Facture::TYPE_SITUATION)
+ {
+ $source_fk_prev_id = $line->fk_prev_id; // temporary storing situation invoice fk_prev_id
+ $line->fk_prev_id = $line->id; // The new line of the new credit note we are creating must be linked to the situation invoice line it is created from
+ if (!empty($invoice->tab_previous_situation_invoice))
+ {
+ // search the last standard invoice in cycle and the possible credit note between this last and invoice
+ // TODO Move this out of loop of $invoice->lines
+ $tab_jumped_credit_notes = array();
+ $lineIndex = count($invoice->tab_previous_situation_invoice) - 1;
+ $searchPreviousInvoice = true;
+ while ($searchPreviousInvoice)
+ {
+ if ($invoice->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_SITUATION || $lineIndex < 1)
+ {
+ $searchPreviousInvoice = false; // find, exit;
+ break;
+ } else {
+ if ($invoice->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_CREDIT_NOTE) {
+ $tab_jumped_credit_notes[$lineIndex] = $invoice->tab_previous_situation_invoice[$lineIndex]->id;
+ }
+ $lineIndex--; // go to previous invoice in cycle
+ }
+ }
+
+ $maxPrevSituationPercent = 0;
+ foreach ($invoice->tab_previous_situation_invoice[$lineIndex]->lines as $prevLine)
+ {
+ if ($prevLine->id == $source_fk_prev_id)
+ {
+ $maxPrevSituationPercent = max($maxPrevSituationPercent, $prevLine->situation_percent);
+
+ //$line->subprice = $line->subprice - $prevLine->subprice;
+ $line->total_ht = $line->total_ht - $prevLine->total_ht;
+ $line->total_tva = $line->total_tva - $prevLine->total_tva;
+ $line->total_ttc = $line->total_ttc - $prevLine->total_ttc;
+ $line->total_localtax1 = $line->total_localtax1 - $prevLine->total_localtax1;
+ $line->total_localtax2 = $line->total_localtax2 - $prevLine->total_localtax2;
+
+ $line->multicurrency_subprice = $line->multicurrency_subprice - $prevLine->multicurrency_subprice;
+ $line->multicurrency_total_ht = $line->multicurrency_total_ht - $prevLine->multicurrency_total_ht;
+ $line->multicurrency_total_tva = $line->multicurrency_total_tva - $prevLine->multicurrency_total_tva;
+ $line->multicurrency_total_ttc = $line->multicurrency_total_ttc - $prevLine->multicurrency_total_ttc;
+ }
+ }
+
+ // prorata
+ $line->situation_percent = $maxPrevSituationPercent - $line->situation_percent;
+
+ //print 'New line based on invoice id '.$invoice->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'
';
+
+ // If there is some credit note between last situation invoice and invoice used for credit note generation (note: credit notes are stored as delta)
+ $maxPrevSituationPercent = 0;
+ foreach ($tab_jumped_credit_notes as $index => $creditnoteid) {
+ foreach ($invoice->tab_previous_situation_invoice[$index]->lines as $prevLine)
+ {
+ if ($prevLine->fk_prev_id == $source_fk_prev_id)
+ {
+ $maxPrevSituationPercent = $prevLine->situation_percent;
+
+ $line->total_ht -= $prevLine->total_ht;
+ $line->total_tva -= $prevLine->total_tva;
+ $line->total_ttc -= $prevLine->total_ttc;
+ $line->total_localtax1 -= $prevLine->total_localtax1;
+ $line->total_localtax2 -= $prevLine->total_localtax2;
+
+ $line->multicurrency_subprice -= $prevLine->multicurrency_subprice;
+ $line->multicurrency_total_ht -= $prevLine->multicurrency_total_ht;
+ $line->multicurrency_total_tva -= $prevLine->multicurrency_total_tva;
+ $line->multicurrency_total_ttc -= $prevLine->multicurrency_total_ttc;
+ }
+ }
+ }
+
+ // prorata
+ $line->situation_percent += $maxPrevSituationPercent;
+
+ //print 'New line based on invoice id '.$invoice->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'
';
+ }
+ }
+
+ $line->fk_facture = $creditnote->id;
+ $line->fk_parent_line = $fk_parent_line;
+
+ $line->subprice = -$line->subprice; // invert price for object
+ $line->pa_ht = $line->pa_ht; // we choosed to have buy/cost price always positive, so no revert of sign here
+ $line->total_ht = -$line->total_ht;
+ $line->total_tva = -$line->total_tva;
+ $line->total_ttc = -$line->total_ttc;
+ $line->total_localtax1 = -$line->total_localtax1;
+ $line->total_localtax2 = -$line->total_localtax2;
+
+ $line->multicurrency_subprice = -$line->multicurrency_subprice;
+ $line->multicurrency_total_ht = -$line->multicurrency_total_ht;
+ $line->multicurrency_total_tva = -$line->multicurrency_total_tva;
+ $line->multicurrency_total_ttc = -$line->multicurrency_total_ttc;
+
+ $result = $line->insert(0, 1); // When creating credit note with same lines than source, we must ignore error if discount alreayd linked
+
+ $creditnote->lines[] = $line; // insert new line in current object
+
+ // Defined the new fk_parent_line
+ if ($result > 0 && $line->product_type == 9) {
+ $fk_parent_line = $result;
+ }
+ }
+ $creditnote->update_price(1);
+
+ $constantforkey = 'CASHDESK_NO_DECREASE_STOCK'.$_SESSION["takeposterminal"];
+ if (!empty($conf->stock->enabled) && $conf->global->$constantforkey != "1") {
+ $savconst = $conf->global->STOCK_CALCULATE_ON_BILL;
+ $conf->global->STOCK_CALCULATE_ON_BILL = 1;
+ $constantforkey = 'CASHDESK_ID_WAREHOUSE'.$_SESSION["takeposterminal"];
+ dol_syslog("Validate invoice with stock change into warehouse defined into constant ".$constantforkey." = ".$conf->global->$constantforkey);
+ $batch_rule = 0;
+ if (!empty($conf->productbatch->enabled) && !empty($conf->global->CASHDESK_FORCE_DECREASE_STOCK)) {
+ require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php';
+ $batch_rule = Productbatch::BATCH_RULE_SELLBY_EATBY_DATES_FIRST;
+ }
+ $res = $creditnote->validate($user, '', $conf->global->$constantforkey, 0, $batch_rule);
+ $conf->global->STOCK_CALCULATE_ON_BILL = $savconst;
+ } else {
+ $res = $creditnote->validate($user);
+ }
+}
+
if ($action == 'history')
{
$placeid = (int) GETPOST('placeid', 'int');
@@ -785,6 +933,11 @@ function DolibarrTakeposPrinting(id) {
});
}
+function CreditNote() {
+ $("#poslines").load("invoice.php?action=creditnote&invoiceid="+placeid, function() {
+ });
+}
+
$( document ).ready(function() {
console.log("Set customer info and sales in header");
@@ -1162,6 +1315,10 @@ if ($placeid > 0)
print '';
+if ($action == "valid" || $action == "history"){
+ print '';
+}
+
if ($action == "search")
{
From a66be11e71dc4248191db53141accc7bdde9b28b Mon Sep 17 00:00:00 2001
From: javierybar <36415318+javierybar@users.noreply.github.com>
Date: Sun, 25 Oct 2020 20:06:50 +0100
Subject: [PATCH 2/3] Update index.php
---
htdocs/takepos/index.php | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/htdocs/takepos/index.php b/htdocs/takepos/index.php
index 83a8fc7931b..a30ccc2935f 100644
--- a/htdocs/takepos/index.php
+++ b/htdocs/takepos/index.php
@@ -25,13 +25,13 @@
//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); // Not disabled cause need to load personalized language
//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); // Not disabled cause need to load personalized language
-//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
-//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
-if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
+//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
+//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
+if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1');
-if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
-if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
-if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
+if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
+if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
+if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
require '../main.inc.php'; // Load $user and permissions
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
From d23b6e145a9f3afa328c285e47303a2bef0fe0e6 Mon Sep 17 00:00:00 2001
From: javierybar <36415318+javierybar@users.noreply.github.com>
Date: Sun, 25 Oct 2020 20:24:17 +0100
Subject: [PATCH 3/3] Fixes
---
htdocs/takepos/invoice.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/htdocs/takepos/invoice.php b/htdocs/takepos/invoice.php
index 00a227e8041..8975c7426c3 100644
--- a/htdocs/takepos/invoice.php
+++ b/htdocs/takepos/invoice.php
@@ -281,7 +281,7 @@ if ($action == 'creditnote')
$creditnote->remise_absolue = $invoice->remise_absolue;
$creditnote->remise_percent = $invoice->remise_percent;
$creditnote->create($user);
-
+
foreach ($invoice->lines as $line)
{
// Extrafields
@@ -400,7 +400,7 @@ if ($action == 'creditnote')
}
}
$creditnote->update_price(1);
-
+
$constantforkey = 'CASHDESK_NO_DECREASE_STOCK'.$_SESSION["takeposterminal"];
if (!empty($conf->stock->enabled) && $conf->global->$constantforkey != "1") {
$savconst = $conf->global->STOCK_CALCULATE_ON_BILL;