Download FV asDocument from E5 CRM; E-Commerce: fix rewizor encoding

This commit is contained in:
2025-06-05 20:02:44 +00:00
parent 7e46fc97b6
commit 0c87d23283
11 changed files with 666 additions and 503 deletions

View File

@@ -1,7 +1,8 @@
<?php <?php
// preDb_0dcc87940d3655fa574b253df04ca1c3 // preDb_0dcc87940d3655fa574b253df04ca1c3
// %g7!@fG // %g7!@fG
function createPzFromInvoice($record, $stockId) { function createPzFromInvoice($record, $stockId)
{
global $app_list_strings; global $app_list_strings;
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
@@ -133,7 +134,8 @@ function sendStocks()
} }
echo json_encode($stocks); echo json_encode($stocks);
} }
function sendSale($record) { function sendSale($record)
{
require_once('modules/EcmSales/EcmSale.php'); require_once('modules/EcmSales/EcmSale.php');
$sale = new EcmSale(); $sale = new EcmSale();
$sale->retrieve($record); $sale->retrieve($record);
@@ -167,15 +169,86 @@ function sendSale($record) {
); );
echo json_encode($response); echo json_encode($response);
} }
function updateSaleE5Number($record, $e5_record, $e5_document_no)
function updateSaleE5Number($record, $e5_record, $e5_document_no) { {
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
$db->query("UPDATE ecmsales SET edi_zs_id='$e5_record', edi_zs_document_no='$e5_document_no' WHERE id='$record'"); $db->query("UPDATE ecmsales SET edi_zs_id='$e5_record', edi_zs_document_no='$e5_document_no' WHERE id='$record'");
die(); die();
} }
function createCostDocumentFromInvoice($record)
{
global $app_list_strings;
$db = $GLOBALS['db'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_URL, "https://crm.e5.pl/REST/index.php?key=7e50a8a5-f01f-4fbc-8c1b-59f3fc474bb5&action=getInvoice&record=" . $record);
$res = curl_exec($curl);
$inv = json_decode($res);
$inv->position_list = json_decode($inv->position_list);
echo 'Tworzę dokument kosztowy w Twinpol<br>';
echo 'Numer faktury: ' . $inv->document_no . '<br>';
removeDocumentIfExists($inv->document_no);
$doc = new Document();
$doc->document_name = $inv->document_no;
$doc->document_number = $inv->document_no;
$doc->doc_symbol = $inv->document_no;
$doc->revision = 1;
$doc->category_id = 'revision_note';
$doc->status = '2';
$doc->transaction_type = '1';
$doc->document_type = 'k';
$doc->active_date = $inv->register_date;
$doc->payment_date = $inv->payment_date;
$doc->document_date = $inv->register_date;
$doc->date_entered = date('Y-m-d H:i:s');
$doc->date_modified = date('Y-m-d H:i:s');
$doc->value = $inv->total_brutto;
$doc->left_paid = $inv->total_brutto;
$doc->currency_id = $inv->currency_id;
$doc->currency_value = 1;
$id = $doc->save();
$parents = array();
$parent = array();
$parent['id'] = create_guid();
$parent['name'] = 'E5 Polska Sp. z o.o.';
$parent['parent_id'] = 'f084e64a-4e63-a3d1-6417-58cbf730df3f';
$parent['parent_type'] = 'Accounts';
$parents[] = $parent;
$doc->saveParentList($parents);
$vats = array();
$vatList = explode(',', $inv->vats_summary);
foreach ($vatList as $vatItem) {
if (empty($vatItem)) continue;
$parts = explode(':', $vatItem);
if (count($parts) != 5) continue;
$vat = array();
$vat['netto'] = $parts[1];
$vat['vat'] = $parts[2];
$vat['vat_id'] = str_replace('%', '', $parts[0]);
$vat['vat_value'] = $parts[0];
$vats[] = $vat;
}
$doc->saveVatList($vats);
echo 'Utworzono dokument kosztowy: <a target="new" href="https://crm.twinpol.com/index.php?module=Documents&action=DetailView&record=' . $id . '">' . $doc->document_name . '</a><br>';
}
// local helpers // local helpers
function getProduct($code) { function getProduct($code)
{
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE code='$code' AND deleted=0")); $res = $db->fetchByAssoc($db->query("SELECT * FROM ecmproducts WHERE code='$code' AND deleted=0"));
if (!$res) { if (!$res) {
@@ -187,6 +260,7 @@ function getProduct($code) {
return $prod; return $prod;
} }
} }
/*
function createProduct($record) { function createProduct($record) {
$curl = curl_init(); $curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET' );
@@ -221,11 +295,21 @@ function createProduct($record) {
//$new->save(); //$new->save();
return $new; return $new;
} }
function brecho($msg) { */
function removeDocumentIfExists($document_number)
{
$db = $GLOBALS['db'];
$res = $db->fetchByAssoc($db->query("SELECT id FROM documents WHERE document_number='$document_number'"));
if ($res) {
echo 'Istnieje już dokument kosztowy o numerze ' . $document_number . '. Usuwam go...<br>';
$db->query("DELETE FROM documents WHERE id='" . $res['id'] . "'");
$db->query("DELETE FROM documents_accounts WHERE document_id='" . $res['id'] . "'");
$db->query("DELETE FROM documents_vat WHERE document_id='" . $res['id'] . "'");
}
}
function brecho($msg)
{
echo '<br><br>'; echo '<br><br>';
var_dump($msg); var_dump($msg);
echo '<br><br>'; echo '<br><br>';
} }
// mysql create table with fields id varchar, created_at datetime, and payload max text
// create table e5_logs (id varchar(255), created_at datetime, payload text);

View File

@@ -35,5 +35,9 @@
case 'updateSaleE5Number': case 'updateSaleE5Number':
updateSaleE5Number($_GET['record'], $_GET['e5_record'], $_GET['e5_document_no']); updateSaleE5Number($_GET['record'], $_GET['e5_record'], $_GET['e5_document_no']);
break; break;
case 'createCostDocumentFromInvoice':
createCostDocumentFromInvoice($_GET['record']);
break;
} }
// https://crm.twinpol.com/REST/index.php?key=d68dac4c-f784-4e1b-8267-9ffcfa0eda4c&action=createCostDocumentFromInvoice&record=c3f6eaa6-0cbd-8c89-1a8c-683ff19a36db
?> ?>

View File

@@ -67,6 +67,13 @@ class Document extends SugarBean {
var $related_doc_rev_number; var $related_doc_rev_number;
var $is_template; var $is_template;
var $template_type; var $template_type;
var $payment_date;
var $transaction_type;
var $document_type;
var $value;
var $left_paid;
var $currency_id;
var $document_number;
//additional fields. //additional fields.
var $revision; var $revision;
var $last_rev_create_date; var $last_rev_create_date;
@@ -354,6 +361,7 @@ class Document extends SugarBean {
$return_array [] = $w; $return_array [] = $w;
} }
$json = getJSONobj(); $json = getJSONobj();
var_dump($return_array);
return $array ? $return_array : $json->encode($return_array); return $array ? $return_array : $json->encode($return_array);
} }
} }

View File

@@ -140,6 +140,11 @@ $dictionary ['Document'] = array (
'required' => true, 'required' => true,
'importable' => 'required' 'importable' => 'required'
), ),
'document_number' => array(
'name' => 'document_name',
'type' => 'varchar',
'len' => '45',
),
'name' => array( 'name' => array(
'name' => 'name', 'name' => 'name',
'vname' => 'LBL_NAME', 'vname' => 'LBL_NAME',
@@ -493,4 +498,3 @@ $dictionary ['Document'] = array (
VardefManager::createVardef('Documents', 'Document', array( VardefManager::createVardef('Documents', 'Document', array(
'default' 'default'
)); ));
?>

View File

@@ -104,6 +104,8 @@ src="modules/Documents/js/Documents13.js"></script><input type="hidden" id="dbas
if(strtotime($this->bean->date_entered.'+1 hours')<strtotime("now")){ if(strtotime($this->bean->date_entered.'+1 hours')<strtotime("now")){
// die('Brak możliwości edycji'); // die('Brak możliwości edycji');
} }
var_dump("TREST");
var_dump($this->bean->vats);
} else { } else {
global $timedate; global $timedate;
$format = $timedate->get_cal_date_format(); $format = $timedate->get_cal_date_format();

View File

@@ -24,22 +24,46 @@ $(document).ready(function () {
}); });
function exportToRewizor(source, date, type) { function exportToRewizor(source, date, type) {
if (type == undefined || type == null || type == "") {
alert("Wybierz rodzaj faktur (normalne lub korekty).");
return;
}
if (source == undefined || source == null || source == "") {
alert("Wybierz źródło faktur.");
return;
}
blockUI("Trwa generowanie pliku..."); blockUI("Trwa generowanie pliku...");
$.ajax({
url: "index.php?module=EcmInvoiceOuts&action=ecommerce&ajax=exportToRewizor&to_pdf=1&source="+source+"&date="+date+"&type="+type, var xhr = new XMLHttpRequest();
type: "GET", xhr.open('GET', "index.php?module=EcmInvoiceOuts&action=ecommerce&ajax=exportToRewizor&to_pdf=1&source="+source+"&date="+date+"&type="+type, true);
success: function (data) { xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (xhr.status === 200) {
var arrayBuffer = xhr.response;
var bytes = new Uint8Array(arrayBuffer);
var data = '';
for (var i = 0; i < bytes.length; i++) {
data += String.fromCharCode(bytes[i]);
}
data = data.replace(/ {4}/g, ""); data = data.replace(/ {4}/g, "");
data = data.replace(/\n/g, "\r\n"); data = data.replace(/\n/g, "\r\n");
var bytes = new TextEncoder("windows-1252", { NONSTANDARD_allowLegacyEncoding: true }).encode(data);
var filename = "rewizor-allegro.epp"; var finalBytes = new Uint8Array(data.length);
var blob = new Blob([bytes], { type: "text/csv"}); for (var i = 0; i < data.length; i++) {
finalBytes[i] = data.charCodeAt(i) & 0xFF;
}
var filename = `rewizor-${source}-${type}.epp`;
var blob = new Blob([finalBytes], { type: "text/plain;charset=iso-8859-2"});
if (navigator.msSaveBlob) { // IE 10+ if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename); navigator.msSaveBlob(blob, filename);
} else { } else {
var link = document.createElement("a"); var link = document.createElement("a");
if (link.download !== undefined) { // feature detection if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob); var url = URL.createObjectURL(blob);
link.setAttribute("href", url); link.setAttribute("href", url);
link.setAttribute("download", filename); link.setAttribute("download", filename);
@@ -48,16 +72,21 @@ function exportToRewizor(source, date, type) {
link.click(); link.click();
document.body.removeChild(link); document.body.removeChild(link);
} }
$.unblockUI();
} }
$.unblockUI(); $.unblockUI();
},
error: function () { } else {
window.alert("Błąd eksportu."); window.alert("Błąd eksportu.");
$.unblockUI(); $.unblockUI();
} }
}); };
xhr.onerror = function() {
window.alert("Błąd eksportu.");
$.unblockUI();
};
xhr.send();
} }
function blockUI($msg) { function blockUI($msg) {

View File

@@ -53,12 +53,18 @@ function show()
function getInvoices($source, $date, $type) function getInvoices($source, $date, $type)
{ {
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
if ($source =='') $source = '%';
$date .= '%'; $date .= '%';
$query = "SELECT i.*, wz.document_no AS wz_document_no, wz.id AS wz_id FROM ecommerce_invoices AS i $query = "SELECT i.*, wz.document_no AS wz_document_no, wz.id AS wz_id FROM ecommerce_invoices AS i
LEFT JOIN ecmstockdocouts AS wz on wz.id = i.ecmstockdocout_id LEFT JOIN ecmstockdocouts AS wz on wz.id = i.ecmstockdocout_id
"; ";
$query .= " WHERE i.origin LIKE '$source' AND i.register_date LIKE '$date'"; $query .= " WHERE i.register_date LIKE '$date'";
if ($source != '') {
if ($source == 'baselinker') {
$query .= " AND i.origin IN ('allegro', 'shop')";
} else {
$query .= " AND i.origin = '$source'";
}
}
if ($type != '') { if ($type != '') {
$query .= " AND i.type='$type'"; $query .= " AND i.type='$type'";
} }
@@ -99,10 +105,11 @@ function getDates()
function getSources() { function getSources() {
$db = $GLOBALS['db']; $db = $GLOBALS['db'];
$sources = array(); $sources = array();
$res = $db->query("SELECT DISTINCT origin FROM ecommerce_invoices"); $res = $db->query("SELECT DISTINCT origin FROM ecommerce_invoices WHERE origin NOT IN ('allegro', 'shop')");
while ($row = $db->fetchByAssoc($res)) { while ($row = $db->fetchByAssoc($res)) {
$sources[]= $row['origin']; $sources[]= $row['origin'];
} }
$sources[] = 'baselinker';
return $sources; return $sources;
} }
@@ -125,14 +132,13 @@ function exportToRewizor($source, $date, $type)
} }
$smarty->assign("data", $invoices); $smarty->assign("data", $invoices);
$result = '';
if ($type == 'normal') { if ($type == 'normal') {
$result = $smarty->display(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor.tpl'); $result = $smarty->fetch(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor.tpl');
} else { } else {
$result = $smarty->display(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor_fvkor.tpl'); $result = $smarty->fetch(getcwd() . '/modules/EcmInvoiceOuts/BimIT-eCommerceInvoices/rewizor_fvkor.tpl');
} }
// encode result as ansi echo mb_convert_encoding($result, 'ISO-8859-2', 'UTF-8');
echo $result;
//echo mb_convert_encoding($result, 'ANSI', 'UTF-8');
} }
function brecho($var) function brecho($var)

View File

@@ -165,6 +165,8 @@ class EcmProduct extends SugarBean {
var $position_list; var $position_list;
var $th; var $th;
var $ean; var $ean;
var $position_list3;
var $modified_user_name;
// RELATED FIELDS // RELATED FIELDS
var $created_by; var $created_by;
var $created_by_name; var $created_by_name;

View File

@@ -14,7 +14,8 @@ class PurchaseCreator{
} }
public function getPurchases($type){ public function getPurchases($type){
$query="select * from documents where document_date>='" . $this->date_from . "' and document_date<='" . $this->date_to . "' and category_id in ('invoice','compain_note','return_note') and deleted=0"; //$query = "SELECT * FROM documents WHERE id='790f2a4d-28d7-e135-9be2-68408ca7279a'";
//$query="select * from documents where document_date>='" . $this->date_from . "' and document_date<='" . $this->date_to . "' and category_id in ('invoice','compain_note','return_note') and deleted=0";
if ($type == "new") { if ($type == "new") {
$query .= " and exported = 0"; $query .= " and exported = 0";
} }

View File

@@ -148,6 +148,7 @@ class EcmSale extends SugarBean {
var $pdf_type; var $pdf_type;
var $pdf_text; var $pdf_text;
var $ecommerce_invoices; var $ecommerce_invoices;
var $vats_summary;
// SUBPANELS RELATED // SUBPANELS RELATED
// MODULE OBJECT DETAILS // MODULE OBJECT DETAILS
var $module_dir = "EcmSales"; var $module_dir = "EcmSales";

View File

@@ -77,6 +77,28 @@ class EcmStockDocIn extends SugarBean {
var $stock_id; var $stock_id;
var $parent_id; var $parent_id;
var $parent_name; var $parent_name;
var $parent_address_street;
var $parent_address_postalcode;
var $parent_address_city;
var $parent_address_country;
var $parent_nip;
var $status;
var $kind;
var $total_netto;
var $total_brutto;
var $canceled;
var $country_transport_val;
var $foreign_transport_val;
var $foreign_transport_cur_id;
var $country_transport_cur_id;
var $addition_cost_val;
var $addition_cost_cur_id;
var $currency_id;
var $currency_value;
var $currency_sad_value;
var $vats_summary;
var $date_fk;
var $fromREST;
// SUBPANELS RELATED // SUBPANELS RELATED
// MODULE OBJECT DETAILS // MODULE OBJECT DETAILS
var $module_dir = "EcmStockDocIns"; var $module_dir = "EcmStockDocIns";