Files
crm.twinpol.com/modules/Schedulers/importEdiOrders.php

142 lines
3.4 KiB
PHP
Raw Normal View History

2025-05-12 15:44:39 +00:00
<?php
/*
require_once ('modules/EcmSales/EcmSale.php');
require_once ('modules/EcmSales/readXML.php');
ini_set("max_execution_time",-1);
ini_set('display_errors',1);
class orderEditor{
public $sXe;
public $file2;
public function __construct($file,$file2){
copy($file,$file2);
$this->file2=$file2;
$this->sXe = new SimpleXMLElement(file_get_contents($file2), LIBXML_NOEMPTYTAG);
}
public function modifyBuyer(){
$this->sXe->{"Order-Parties"}->Buyer->ILN='5909000837119';
}
public function saveFile(){
$content = $this->sXe->asXML();
file_put_contents($this->file2,str_replace('<?xml version="1.0"?>',"",$content));
}
public function sendFile(){
$connection = ssh2_connect('crm.e5.pl', 22);
ssh2_auth_password($connection, 'mz', '56!@HjU&3aZ');
$sftp = ssh2_sftp($connection);
ssh2_scp_send($connection, $this->file2, '/home/mz/edi/files/orders/'.uniqid().'.xml', 0775);
}
}
$path = "/home/saas/orders/";
$folders = array (
'archive',
'temp',
'confirm',
'omitted',
'sent',
'failed',
'e5'
); // do not read this folders
// read path with sales
$orders = array ();
if (is_dir ( $path )) {
if ($dh = opendir ( $path )) {
while ( ($file = readdir ( $dh )) !== false ) {
if (! is_dir ( $file ) && ! in_array ( $file, $folders )) {
$ext=substr($file, -3);
if($ext=='.ok'){
unlink($path.$file);
}
$orders [] = $file;
}
}
}
}
// add orders
$mail_msg = array ();
foreach ( $orders as $order ) {
$s = new EcmSale ();
$reader = new readXML ();
$reader->loadXMLFile ( $order );
$reader->loader ( $s );
$s->position_list = $reader->getPositionList ( true );
$number = $s->generateNumber ();
$check = new EcmSale ();
$check->retrieve_by_string_fields(['parent_document_no'=>$s->parent_document_no ]);
if($check->id!==null)continue;
$s->number = $number;
$s->document_no = $s->formatNumber ( $number );
$s->edi_file = $order;
$s->type = 'sales_order';
$s->status = 's10';
$s->ecmlanguage = 'pl_pl';
$a = new Account ();
$a->retrieve ( $s->parent_id );
$s->assigned_user_id = $a->assigned_user_id;
$s->assigned_user_name = $a->assigned_user_name;
// calculate totals
$sum_netto = 0;
$sum_brutto = 0;
foreach ( $s->position_list as $p ) {
$sum_netto += $p ['total_netto'];
$sum_brutto += $p ['total_brutto'];
}
$s->total_netto = $sum_netto;
$s->total_brutto = $sum_brutto;
$s->save ();
if (! is_array ( $mail_msg [$a->assigned_user_id] ))
$mail_msg [$a->assigned_user_id] = array ();
if (! $s->parent_name || $s->parent_name == "")
$s->parent_name = 'Błąd! Brak kontrahenta!';
$mail_msg [$a->assigned_user_id] [] = $s->document_no . ', ' . $s->parent_document_no . ', ' . $s->parent_name;
unset ( $a );
// move files
$ob = new orderEditor($path . '' . $order, $path . 'e5/' . $order );
$ob->modifyBuyer();
$ob->saveFile();
$ob->sendFile();
rename ( $path . '' . $order, $path . 'archive/' . $order );
}
foreach ( $mail_msg as $user => $msg ) {
$u = new User ();
$u->retrieve ( $user );
$message = $date = date ( 'Y-m-d H:i:s' ) . ' dodano zamówienie(a):<br>';
foreach ( $msg as $m ) {
$message .= $m . '<br>';
}
require_once 'include/ECM/EcmSendPdfButton/EcmSendPdfButton.inc';
$t = new EcmSendPdfButton('Users', $usr_id,'mf@e5.pl',$type);
#$t->setAdress('mf@e5.pl');
$t->setAdress('outtervision@gmail.com');
$t->setSubject( "Import zamówienia EDI");
$t->setBody($message);
$success = false;
$success= $t->sendEmail();
//return true;
}
*/
return true;
?>