Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.38 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { NavController } from '@ionic/angular';
  3. import { HomePage } from '../home/home.page';
  4. import { BarcodeScannerOptions, BarcodeScanner } from "@ionic-native/barcode-scanner/ngx";
  5. import { HTTP } from '@ionic-native/http/ngx';
  6. import { HttpClient } from '@angular/common/http';
  7. //import { Promise } from 'es6-promise';
  8. import { Platform } from '@ionic/angular';
  9. import { AlertController } from '@ionic/angular';
  10. import { DatetimeChangeEventDetail } from '@ionic/core';
  11. import { ViewState } from '@angular/core/src/view';
  12.  
  13.  
  14. import pdfMake from 'pdfmake/build/pdfmake';
  15. import pdfFonts from 'pdfmake/build/vfs_fonts';
  16. pdfMake.vfs = pdfFonts.pdfMake.vfs;
  17.  
  18.  
  19. import { File } from '@ionic-native/file/ngx';
  20. import { FileOpener } from '@ionic-native/file-opener/ngx';
  21. import { forEach } from '@angular/router/src/utils/collection';
  22.  
  23. import { EmailComposer } from '@ionic-native/email-composer/ngx';
  24. import { SPINNERS } from '@ionic/core/dist/types/components/spinner/spinner-configs';
  25. import { Content } from '@angular/compiler/src/render3/r3_ast';
  26.  
  27. @Component({
  28. selector: 'app-stock',
  29. templateUrl: './stock.page.html',
  30. styleUrls: ['./stock.page.scss'],
  31. })
  32.  
  33. export class StockPage implements OnInit {
  34. mode: any = window.sessionStorage.getItem("mode");
  35. username: any = window.sessionStorage.getItem("username");
  36. userid: any = window.sessionStorage.getItem("userid");
  37. selectBox: any = null;
  38. set_selectBox: any = null;
  39. id_selectBox: any = null;
  40. box: any = null;
  41. set_box: any = null;
  42. boxState: string[] = ['Goala', 'Plina'];
  43. set_boxState: any = null;
  44. exista: any = null;
  45. serie: any = null;
  46. serie_show: any = null;
  47. volum: any = null;
  48. volum_total: any = null;
  49. diametru: any = null;
  50. lungime: any = null;
  51. tableDet: any = null;
  52. quality: any = null;
  53. stockList: any = [];
  54. sterge_serie: any = null;
  55. comment1: string = null;
  56. comment2: string = null;
  57. boxa_ini: any = null;
  58. //username: any = window.sessionStorage.getItem("username");
  59. receptie: any = null;
  60. server: any = null;
  61. pdfObj: any = null;
  62. pdfrow: any = [];
  63. pdfbody: any = [];
  64. lista_receptie: any = [];
  65. pdfspec: any = null;
  66. pdftype: any = null;
  67. pdffurnizor: any = null;
  68. pdfvaloarebustean: any = null;
  69. pdfvaloaretotala: any = null;
  70. pdfidrec: any = null;
  71. registerYes: number = 0;
  72. registerNo: number = 0;
  73. mailYes: number = 0;
  74. modeProvider: any = null;
  75. provider: any = null;
  76. set_provider: any = null;
  77. provider_result: any = null;
  78. providerAviz: any = null;
  79. getAllFurnizor: any = null;
  80. set_Furnizor: any = null;
  81. woodtype: any = null;
  82. finish_download: any = null;
  83. email: any = null;
  84. emailtosend: any;
  85.  
  86.  
  87.  
  88.  
  89. dateStorage: string = new Date().toISOString();
  90.  
  91. constructor(public navCtrl: NavController, private barcodeScanner: BarcodeScanner, public http: HttpClient, public platform: Platform, private alertCtrl: AlertController,
  92. private file: File, private fileOpener: FileOpener, private emailComposer: EmailComposer) {
  93. this.backButtonEvent();
  94. this.getBox();
  95.  
  96. }
  97.  
  98. ngOnInit() {
  99. }
  100.  
  101. meniu() {
  102. this.navCtrl.navigateRoot('/meniu');
  103. }
  104.  
  105. backButtonEvent() {
  106. this.platform.backButton.subscribeWithPriority(999999, () => {
  107. //alert("Back Button");
  108. this.navCtrl.navigateRoot('/meniu');
  109.  
  110. });
  111. }
  112.  
  113. scanCode() {
  114. this.barcodeScanner
  115. .scan()
  116. .then(barcodeData => {
  117. this.serie_show = barcodeData.text;
  118. var xhttp = new XMLHttpRequest();
  119. xhttp.responseType = 'json';
  120. var mydata = new FormData();
  121. mydata.append("action", "cautare");
  122. mydata.append("serie", barcodeData.text);
  123.  
  124. xhttp.open("POST", "https://services.sat1.ro/smartwood/scanStock.php", true);
  125. xhttp.send(mydata);
  126.  
  127. var a = new Promise((resolve, reject) => {
  128. xhttp.onreadystatechange = () => {
  129. if (xhttp.readyState == 4 && xhttp.status == 200) {
  130. this.exista = xhttp.response.exista;
  131. console.log("serie exista: " + this.exista)
  132. if (this.exista == "DA") {
  133. this.serie = barcodeData.text;
  134. this.getWoodBySeries();
  135. }
  136. else {
  137. alert("Serie incorecta!")
  138. }
  139. }
  140.  
  141. }
  142. });
  143.  
  144. })
  145. .catch(err => {
  146. console.log("Error", err);
  147. });
  148.  
  149. this.serie = "";
  150. this.exista = ""; //a
  151.  
  152.  
  153.  
  154. }
  155.  
  156. getBox() {
  157.  
  158. var xhttp = new XMLHttpRequest();
  159. xhttp.responseType = 'json';
  160.  
  161. var mydata = new FormData();
  162. mydata.append("action", "box");
  163. xhttp.open("POST", "https://services.sat1.ro/smartwood/get_box.php", true);
  164. xhttp.send(mydata);
  165.  
  166. var a = new Promise((resolve, reject) => {
  167. xhttp.onreadystatechange = () => {
  168. if (xhttp.readyState == 4 && xhttp.status == 200) {
  169.  
  170. this.box = xhttp.response;
  171.  
  172. }
  173. }
  174. });
  175. }
  176.  
  177. sterge_bustean(sterge_serie) {
  178. this.sterge_serie = sterge_serie;
  179. if (confirm("Sunteti sigur ca doriti sa stergeti busteanul cu seria: " + sterge_serie + " ?")) {
  180. for (var i = 0; i < this.stockList.length; i++) {
  181. if (this.stockList[i]['series'] == sterge_serie) {
  182. this.stockList.splice(i, 1);
  183. }
  184. }
  185. //volumul total
  186. //this.get_volum_total();
  187.  
  188. }
  189.  
  190. }
  191.  
  192. /* get_volum_total() {
  193. this.volum_total = 0;
  194. for (var i = 0; i < this.stockList.length; i++) {
  195. this.volum_total = +this.volum_total;
  196. this.volum_total = +this.stockList[i]['volume'] + this.volum_total;
  197. this.volum_total = this.volum_total.toFixed(2);
  198. }
  199. }
  200. */
  201. get_selectBox() {
  202. var xhttp = new XMLHttpRequest();
  203. xhttp.responseType = 'json';
  204. var mydata = new FormData();
  205. mydata.append("action", "selectBox");
  206.  
  207. xhttp.open("POST", "https://services.sat1.ro/smartwood/get_receptie.php", true);
  208. xhttp.send(mydata);
  209.  
  210. var a = new Promise((resolve, reject) => {
  211. xhttp.onreadystatechange = () => {
  212. if (xhttp.readyState == 4 && xhttp.status == 200) {
  213. this.selectBox = xhttp.response;
  214. }
  215. }
  216. });
  217. }
  218. stockListPush() {
  219. // alert('Seria introdusa nu exista in baza de date!')
  220. if (this.serie == null) {
  221. alert('Serie incorecta!');
  222. }
  223. else {
  224. this.stockList.push({ "series": this.serie, "diameter": this.diametru, "length": this.lungime, "quality": this.quality, "volume": this.volum, "box": this.boxa_ini});
  225. this.serie = +this.serie + 1;
  226.  
  227. }
  228. }
  229. getWoodBySeries() {
  230. //this.stockListPush();
  231. var xhttp = new XMLHttpRequest();
  232. xhttp.responseType = 'json';
  233.  
  234. var mydata = new FormData();
  235. mydata.append("series", this.serie);
  236.  
  237. xhttp.open("POST", "https://services.sat1.ro/smartwood/getWoodBySeries.php", true);
  238. xhttp.send(mydata);
  239.  
  240. var a = new Promise((resolve, reject) => {
  241. xhttp.onreadystatechange = () => {
  242. if (xhttp.readyState == 4 && xhttp.status == 200) {
  243. this.serie = xhttp.response.series;
  244. this.diametru = xhttp.response.diameter;
  245. this.lungime = xhttp.response.length;
  246. this.quality = xhttp.response.quality;
  247. this.volum = xhttp.response.cubage;
  248. this.boxa_ini = xhttp.response.box;
  249.  
  250. var a = true;
  251.  
  252. if (this.stockList.length >= 0) {
  253. for (var item of this.stockList) {
  254. if (item.series == this.serie) {
  255. alert('Seria exista deja!');
  256. a = false;
  257. break;
  258. }
  259. }
  260. }
  261. if (a) {
  262. this.stockListPush();
  263. }
  264. }
  265. }
  266. });
  267.  
  268. this.serie = "";
  269. this.exista = "";
  270. //this.createpdf();
  271. }
  272.  
  273. inregistrare() {
  274. console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  275. console.dir('sa vedem ce e cu array-ul' );
  276.  
  277. console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  278. /*alert('comentariu 1 = ' + this.comment1 + 'comentariu 2 = ' + this.comment2)
  279. alert('serie = ' + this.serie + '' + 'diametru = ' + this.diametru + '' + 'lungime = ' + this.lungime + '' + 'quality = ' + this.quality + '' + 'volum = ' + this.volum )*/
  280. console.log(this.stockList.length);
  281. if (this.stockList.length > 0 && this.box) {
  282. this.createpdf();
  283.  
  284. var xhttp = new XMLHttpRequest();
  285. xhttp.responseType = 'json';
  286. var mydata = new FormData();
  287.  
  288. mydata.append("inregistrare", "inregistrare");
  289. console.log("stocklist din inregistrare este: " + this.stockList.length)
  290. this.stockList.forEach(function (item, i) {
  291. console.log("seria din stocklist" + item.series)
  292. console.log("diametru din stocklist" + item.diameter)
  293. mydata.append("stockList[" + i + "][serie]", item.series);
  294.  
  295. });
  296.  
  297. mydata.append("data", this.dateStorage);
  298. console.log("datestorage: "+this.dateStorage.toString());
  299. mydata.append("boxa", this.set_box);
  300. mydata.append("stareBoxa", this.set_boxState);
  301. mydata.append("comment1", this.comment1);
  302. mydata.append("comment2", this.comment2);
  303.  
  304. xhttp.open("POST", "https://services.sat1.ro/smartwood/inregistrare.php", true);
  305. xhttp.send(mydata);
  306.  
  307.  
  308. var a = new Promise((resolve, reject) => {
  309. xhttp.onreadystatechange = () => {
  310. if (xhttp.readyState == 4 && xhttp.status == 200) {
  311. this.serie = "";
  312. this.set_boxState = '';
  313. this.set_box = '';
  314. this.comment1 = '';
  315. this.comment2 = '';
  316. this.stockList = [];
  317. alert('Busteanul a fost adaugat!');
  318.  
  319.  
  320. }
  321. }
  322. });
  323. }
  324. else {
  325. alert("Completati toate campurile!");
  326. }
  327.  
  328. }
  329.  
  330.  
  331. async downloadAlert() {
  332. const alert = await this.alertCtrl.create({
  333. message: 'PDF Mailer',
  334. subHeader: 'Doriti sa trimiteti acest fisier?',
  335. buttons: [
  336. {
  337. text: 'Cancel',
  338. role: 'cancel',
  339. handler: () => {
  340. console.log('Cancel clicked');
  341. }
  342. },
  343. {
  344. text: 'Yes',
  345. handler: () => {
  346. console.log('Yes clicked');
  347. this.mailYes = 1;
  348. this.emailSender();
  349. }
  350. }
  351. ]
  352. });
  353. await alert.present();
  354. //setTimeout(() => alert.present(), 7000);
  355. }
  356.  
  357. async openAlert() {
  358. const alert = await this.alertCtrl.create({
  359. message: 'PDF Viewer',
  360. subHeader: 'Doriti sa vizualizati acest fisier?',
  361. buttons: [
  362. {
  363. text: 'Cancel',
  364. role: 'cancel',
  365. handler: () => {
  366. console.log('Cancel clicked');
  367. }
  368. },
  369. {
  370. text: 'Yes',
  371. handler: () => {
  372. console.log('Yes clicked');
  373. this.openPDF();
  374. }
  375. }
  376. ]
  377. });
  378. await alert.present();
  379. //setTimeout(() => alert.present(), 7000);
  380. }
  381.  
  382.  
  383.  
  384. getAttachment() {
  385. this.fileOpener.open('path/to/file.pdf', 'application/pdf')
  386. .then(() => console.log('File is opened'))
  387. .catch(e => console.log('Error opening file', e));
  388. }
  389.  
  390.  
  391. ionViewWillEnter() {
  392. this.server = JSON.parse(window.localStorage.getItem('phploc'));
  393. console.log("phploc sessionstorage= " + this.server);
  394. }
  395.  
  396. createpdf() {
  397.  
  398. //console.log("Create PDF");
  399. this.pdfbody = [];
  400. this.pdfrow = [];
  401. this.pdfvaloaretotala = 0;
  402. this.volum_total = 0;
  403. if (isonline == 'online') {
  404. this.providerAviz = window.sessionStorage.getItem("provider_result");
  405. this.providerAviz = this.providerAviz.replace(/['"]+/g, '');
  406. console.log("in pdf furnizorul din lista show notice = " + this.providerAviz);
  407. }
  408.  
  409. var isonline = window.sessionStorage.getItem('mode');
  410. console.log("ONLINE PDF = " + isonline);
  411.  
  412. console.log("furnizor inainte de if = " + this.modeProvider);
  413.  
  414. this.pdfrow.push([
  415. '',
  416. 'Nr. Aviz',
  417. 'Data: ',
  418. 'Furnizor',
  419. 'Volum/mc'
  420. ]);
  421.  
  422. for (var i = 0; i < this.stockList.length; i++) {
  423. this.pdfbody.push([
  424. i + 1,
  425. this.stockList[i]['series'],
  426. this.stockList[i]['diameter'],
  427. this.stockList[i]['length'],
  428. this.stockList[i]['volume']
  429. ]);
  430.  
  431. ////this.volum_total += this.stockList[i]['volume']
  432. //this.volum_total = 0;
  433. }
  434.  
  435. //this.stockList.forEach(function (item, key) {
  436. // this.pdfbody.push([
  437. // key + 1,
  438. // item.serie,
  439. // item.diameter,
  440. // item.length,
  441. // item.volume,
  442. // ]);
  443. //})
  444.  
  445.  
  446.  
  447.  
  448.  
  449. console.log('asddasasddassdadas');
  450. console.log(this.pdfbody);
  451.  
  452. //this.pdfbody.reverse();
  453.  
  454.  
  455. //this.pdfbody.push(this.pdfrow);
  456.  
  457. var docDefinition = {
  458. content: [
  459. {
  460. table: {
  461. headerRows: 1,
  462. widths: ['*'],
  463. body: [
  464. [{ text: 'Asta e viata mea,mereu vreau sa fie asa', alignment: 'center'}],
  465.  
  466. ],
  467. }
  468. },
  469. {
  470. table: {
  471. headerRows: 1,
  472. widths: ['*', '*'],
  473. body: [
  474. ['Furnizor:', 'Tip lemn'],
  475. ['Nr.aviz:', 'Nr. CMR'],
  476. ['Data aviz:', 'Transportator'],
  477. [{ text: 'Data receptie', alignment: 'left' }, ['Nr. remorca', 'Nr.remorca2200' ]],
  478. ],
  479.  
  480.  
  481. }
  482. },
  483.  
  484.  
  485. ],
  486. styles: {
  487. header: {
  488. fontSize: 24,
  489. bold: true,
  490. },
  491. subheader: {
  492. fontSize: 14,
  493. bold: true,
  494. margin: [0, 15, 0, 0]
  495. },
  496. margintop: {
  497. margin: [0, 20, 0, 0]
  498. },
  499. paddingtop: {
  500. margin: [0, 10, 0, 30]
  501. }
  502.  
  503. }
  504.  
  505. };
  506.  
  507. this.pdfObj = pdfMake.createPdf(docDefinition);
  508. this.downloadPdf();
  509. }
  510.  
  511.  
  512.  
  513. downloadPdf() {
  514. this.pdfObj.download();
  515.  
  516. if (this.platform.is('android')) {
  517. this.file.checkDir(this.file.externalDataDirectory, 'SmartWood')
  518. .then()
  519. .catch(err =>
  520. this.file.createDir(this.file.externalDataDirectory, 'SmartWood', false)
  521. .then()
  522. .catch(err => alert('Nu sa putut crea directoriul'))
  523. );
  524. console.log("2");
  525. this.pdfObj.getBuffer((buffer) => {
  526. var blob = new Blob([buffer], { type: 'application/pdf' });
  527.  
  528. this.file.writeFile(this.file.externalDataDirectory, "SmartWood/" + this.formatDate() + ".pdf", blob, { replace: true }).then((success) => {
  529.  
  530. })
  531. });
  532.  
  533. this.openAlert();
  534. this.downloadAlert();
  535. console.log("3");
  536.  
  537. } else {
  538. this.pdfObj.download();
  539. }
  540. }
  541.  
  542.  
  543. openPDF() {
  544. this.fileOpener.open(this.file.externalDataDirectory + "SmartWood/" + this.formatDate() + ".pdf", 'application/pdf');
  545. }
  546.  
  547. emailSender() {
  548. this.emailtosend = {
  549. to: this.email,
  550. attachments: [
  551. this.file.externalDataDirectory + "SmartWood/" + this.formatDate() + ".pdf"
  552. ],
  553. subject: 'AVIZ ' + this.formatDate(),
  554. body: 'Mai jos aveti pdf- ul cu avize:',
  555. isHtml: true
  556. };
  557. this.emailComposer.open(this.emailtosend);
  558.  
  559.  
  560. }
  561.  
  562.  
  563. formatDate() {
  564. var date = new Date();
  565. var monthNames = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
  566. ];
  567.  
  568. var day = date.getDate();
  569. var monthIndex = date.getMonth();
  570. var year = date.getFullYear();
  571.  
  572. return day + '-' + monthNames[monthIndex] + '-' + year;
  573. }
  574.  
  575. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement