forked from SureShinde/MagentoB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestimportso.php
806 lines (671 loc) · 26.4 KB
/
testimportso.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
<?php
class TestImportSO
{
// change this to either stage or local
protected $destination = 'local';
// number of n latest SOs to be processed
protected $num_latest_so = 100;
// DB config for production
protected $prodhost = 'bilnadbma.csgheamo30fd.ap-southeast-1.rds.amazonaws.com';
protected $produser = 'mybilnauser';
protected $prodpassword = 'LawnqUiT79';
protected $proddb = 'bilna_live';
// DB config for stage (Pokey)
protected $stagehost = 'bilnastage.csgheamo30fd.ap-southeast-1.rds.amazonaws.com';
protected $stageuser = 'mybilnauser';
protected $stagepassword = 'LawnqUiT79';
protected $stagedb = 'bilna_live_stagey';
// DB config for local
protected $localhost = 'localhost';
protected $localuser = 'root';
protected $localpassword = '';
protected $localdb = 'bilna';
// array variables for storing field names of tables
protected $sales_flat_order_fields = array();
protected $sales_flat_order_item_fields = array();
protected $sales_flat_order_grid_fields = array();
protected $sales_flat_order_payment_fields = array();
protected $sales_flat_order_address_fields = array();
protected $sales_flat_order_status_history_fields = array();
protected $sales_flat_invoice_fields = array();
protected $sales_flat_invoice_item_fields = array();
protected $sales_flat_invoice_grid_fields = array();
// array variables for storing values of tables
protected $sales_flat_order_values = array();
protected $sales_flat_order_item_values = array();
protected $sales_flat_order_grid_values = array();
protected $sales_flat_order_payment_values = array();
protected $sales_flat_order_address_values = array();
protected $sales_flat_order_status_history_values = array();
protected $sales_flat_invoice_values = array();
protected $sales_flat_invoice_item_values = array();
protected $sales_flat_invoice_grid_values = array();
// array variables for SO entity ID mapping (production to stage/local)
protected $sales_entity_id_mapper = array();
// array variables for invoice entity ID mapping (production to stage/local)
protected $invoice_entity_id_mapper = array();
// array variables for sales item ID mapping (production to stage/local)
protected $sales_item_id_mapper = array();
// hard coded section
protected $customer_id_hardcoded = 95503;
protected $customer_address_id_hardcoded = 77823;
protected $increment_id_suffix = '-3';
// fields that are excepted to be inserted
protected $fields_excepted = ['sales_flat_order' => ['entity_id', 'cod', 'express_shipping'], 'sales_flat_order_payment' => ['entity_id'],
'sales_flat_invoice' => ['entity_id'], 'sales_flat_order_item' => ['item_id', 'cod', 'express_shipping', 'cross_border'], 'sales_flat_order_address' => ['entity_id'],
'sales_flat_invoice_item' => ['entity_id']];
public function set_server($server)
{
$this->destination = $server;
}
public function set_customer_id($customer_id)
{
$this->customer_id_hardcoded = $customer_id;
}
public function set_customer_address_id($customer_address_id)
{
$this->customer_address_id_hardcoded = $customer_address_id;
}
public function set_no_sos($no_sos)
{
$this->num_latest_so = $no_sos;
}
public function run()
{
$prodlink = $this->connect_production();
// $this->retrieve_fields_from_tables($prodlink); // comment temporarily
// connection is either local or stage
if ($this->destination == 'local')
$destlink = $this->connect_local();
else
if ($this->destination == 'stage')
$destlink = $this->connect_stage();
else
{
echo "DESTINATION ERROR : MUST BE LOCAL OR STAGE ONLY";
exit;
}
/* begin process copying */
$this->copy_so_table($prodlink, $destlink);
$this->copy_sales_flat_order_grid($prodlink, $destlink);
$this->copy_sales_flat_order_item($prodlink, $destlink);
$this->copy_sales_flat_order_address($prodlink, $destlink);
$this->copy_sales_flat_order_payment($prodlink, $destlink);
$this->copy_sales_flat_invoice($prodlink, $destlink);
$this->copy_sales_flat_invoice_grid($prodlink, $destlink);
$this->copy_sales_flat_invoice_item($prodlink, $destlink);
/* copy to table message */
$this->copy_table_message($destlink);
if (count($this->sales_entity_id_mapper) > 0)
{
echo '<h3>Sales That Are Successfully Imported From Production</h3>';
echo '<table><tr><th>No</th><th>Sales Entity ID</th><th>Sales Order No</th></tr>';
$result = mysqli_query($destlink, "SELECT * FROM sales_flat_order WHERE entity_id IN ({$this->convertValuesToCommas($this->sales_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($destlink));
}
if (count($result) > 0)
{
$no = 1;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row['entity_id'].'</td>';
echo '<td>'.$row['increment_id'].'</td>';
echo '</tr>';
$no++;
}
}
echo '</table>';
}
echo '<br /><br />';
if (count($this->invoice_entity_id_mapper) > 0)
{
echo '<h3>Invoices That Are Successfully Imported From Production</h3>';
echo '<table><tr><th>No</th><th>Invoice Entity ID</th><th>Invoice No</th></tr>';
$result = mysqli_query($destlink, "SELECT * FROM sales_flat_invoice WHERE entity_id IN ({$this->convertValuesToCommas($this->invoice_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($destlink));
}
if (count($result) > 0)
{
$no = 1;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row['entity_id'].'</td>';
echo '<td>'.$row['increment_id'].'</td>';
echo '</tr>';
$no++;
}
}
echo '</table>';
}
mysqli_close($prodlink);
mysqli_close($destlink);
}
private function connect_production()
{
$link = mysqli_connect($this->prodhost, $this->produser, $this->prodpassword, $this->proddb);
if (!$link) {
die('Could not connect to Production : ' . mysqli_error($link));
exit;
}
return $link;
}
private function connect_stage()
{
$link = mysqli_connect($this->stagehost, $this->stageuser, $this->stagepassword, $this->stagedb);
if (!$link) {
die('Could not connect to Stage : ' . mysqli_error($link));
exit;
}
return $link;
}
private function connect_local()
{
$link = mysqli_connect($this->localhost, $this->localuser, $this->localpassword, $this->localdb);
if (!$link) {
die('Could not connect to Local : ' . mysqli_error($link));
exit;
}
return $link;
}
private function retrieve_fields_from_tables($link)
{
$this->get_fields($link, 'sales_flat_order', 'sales_flat_order_fields');
$this->get_fields($link, 'sales_flat_order_address', 'sales_flat_order_address_fields');
$this->get_fields($link, 'sales_flat_order_item', 'sales_flat_order_item_fields');
$this->get_fields($link, 'sales_flat_order_grid', 'sales_flat_order_grid_fields');
$this->get_fields($link, 'sales_flat_order_payment', 'sales_flat_order_payment_fields');
$this->get_fields($link, 'sales_flat_order_status_history', 'sales_flat_order_status_history_fields');
$this->get_fields($link, 'sales_flat_invoice', 'sales_flat_invoice_fields');
$this->get_fields($link, 'sales_flat_invoice_item', 'sales_flat_invoice_item_fields');
$this->get_fields($link, 'sales_flat_invoice_grid', 'sales_flat_invoice_grid_fields');
$this->get_fields($link, 'sales_flat_shipment', 'sales_flat_shipment_fields');
$this->get_fields($link, 'sales_flat_shipment_grid', 'sales_flat_shipment_grid_fields');
$this->get_fields($link, 'sales_flat_shipment_item', 'sales_flat_shipment_item_fields');
$this->get_fields($link, 'sales_flat_shipment_track', 'sales_flat_shipment_track_fields');
}
private function get_fields($link, $table, $array_field_var)
{
$fields = array();
$result = mysqli_query($link, "SELECT * FROM $table LIMIT 0, 1");
if (!$result) {
die('Invalid query: ' . mysqli_error($link));
}
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
foreach($row as $key => $value)
$fields[] = $key;
break;
}
$this->{$array_field_var} = $fields;
}
}
/* PROCESSING SALES ORDER */
/* --------------------------------------------------------------------------------------------------- */
private function copy_so_table($prodlink, $destlink)
{
echo "BEGIN COPYING SO FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_order ORDER BY entity_id DESC LIMIT 0, {$this->num_latest_so}");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['customer_id'] = $this->customer_id_hardcoded; // hard code to someone's id
$row['increment_id'] = $row['increment_id'] . $this->increment_id_suffix; // add suffix
$row['netsuite_internal_id'] = ''; // empty the netsuite internal id
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_order');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_order ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_order') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
else
$this->sales_entity_id_mapper[$values['entity_id']] = mysqli_insert_id($destlink);
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING SO FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_order_grid($prodlink, $destlink)
{
echo "BEGIN COPYING SO GRID FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->sales_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_order_grid WHERE entity_id IN ({$this->convertKeysToCommas($this->sales_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['entity_id'] = $this->sales_entity_id_mapper[$row['entity_id']]; // replace the entity ID from the mapper
$row['customer_id'] = $this->customer_id_hardcoded; // hard code to someone's id
$row['increment_id'] = $row['increment_id'] . $this->increment_id_suffix; // add suffix
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row);
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_order_grid ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values) . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING SO GRID FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_order_item($prodlink, $destlink)
{
echo "BEGIN COPYING SO ITEM FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->sales_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_order_item WHERE order_id IN ({$this->convertKeysToCommas($this->sales_entity_id_mapper)}) ORDER BY item_id");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['order_id'] = $this->sales_entity_id_mapper[$row['order_id']]; // replace the order ID from the mapper
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_order_item');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
$already_get_first_item_id = false;
$first_item_id = 0;
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_order_item ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_order_item') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
else
{
$this->sales_item_id_mapper[$values['item_id']] = mysqli_insert_id($destlink);
if (!$already_get_first_item_id)
{
$first_item_id = $this->sales_item_id_mapper[$values['item_id']];
$already_get_first_item_id = true;
}
}
}
// then, we have to update the parent_item_id according to the item_id of destination table
foreach($this->sales_item_id_mapper as $key => $value)
{
// update parent item ID
$updateQuery = "UPDATE sales_flat_order_item SET parent_item_id = $value WHERE parent_item_id = $key AND item_id >= $first_item_id";
$result = mysqli_query($destlink, $updateQuery);
if (!$result)
die('Error update: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING SO ITEM FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_order_address($prodlink, $destlink)
{
echo "BEGIN COPYING SO ADDRESS FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->sales_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_order_address WHERE parent_id IN ({$this->convertKeysToCommas($this->sales_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['parent_id'] = $this->sales_entity_id_mapper[$row['parent_id']]; // replace the parent ID from the mapper
$row['customer_id'] = $this->customer_id_hardcoded;
$row['customer_address_id'] = $this->customer_address_id_hardcoded;
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_order_address');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_order_address ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_order_address') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
$address_type_id = mysqli_insert_id($destlink);
// update the sales order's billing address id and shipping address id
if ($values['address_type'] == 'shipping')
$updateQuery = "UPDATE sales_flat_order SET shipping_address_id = $address_type_id WHERE entity_id = {$values['parent_id']}";
else
if ($values['address_type'] == 'billing')
$updateQuery = "UPDATE sales_flat_order SET billing_address_id = $address_type_id WHERE entity_id = {$values['parent_id']}";
$result = mysqli_query($destlink, $updateQuery);
if (!$result)
die('Error update: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING SO ADDRESS FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_order_payment($prodlink, $destlink)
{
echo "BEGIN COPYING SO PAYMENT FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->sales_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_order_payment WHERE parent_id IN ({$this->convertKeysToCommas($this->sales_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['parent_id'] = $this->sales_entity_id_mapper[$row['parent_id']]; // replace the parent ID from the mapper
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_order_payment');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_order_payment ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_order_payment') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING SO PAYMENT FROM PRODUCTION<br /><br />";
}
/* PROCESSING INVOICE */
/* --------------------------------------------------------------------------------------------------- */
private function copy_sales_flat_invoice($prodlink, $destlink)
{
echo "BEGIN COPYING INVOICE FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_invoice WHERE order_id IN ({$this->convertKeysToCommas($this->sales_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['order_id'] = $this->sales_entity_id_mapper[$row['order_id']]; // replace the order ID from the mapper
$row['increment_id'] = $row['increment_id'] . $this->increment_id_suffix; // add suffix
$row['netsuite_internal_id'] = ''; // empty the netsuite internal id
// get billing address id and shipping address id from sales order
$address_type_ids = $this->get_address_type_ids($row['order_id'], $destlink);
$row['billing_address_id'] = $address_type_ids['billing_address_id'];
$row['shipping_address_id'] = $address_type_ids['shipping_address_id'];
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_invoice');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_invoice ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_invoice') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
else
$this->invoice_entity_id_mapper[$values['entity_id']] = mysqli_insert_id($destlink);
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING INVOICE FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_invoice_grid($prodlink, $destlink)
{
echo "BEGIN COPYING INVOICE GRID FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->invoice_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_invoice_grid WHERE entity_id IN ({$this->convertKeysToCommas($this->invoice_entity_id_mapper)})");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['entity_id'] = $this->invoice_entity_id_mapper[$row['entity_id']]; // replace the entity ID from the mapper
$row['order_id'] = $this->sales_entity_id_mapper[$row['order_id']]; // replace the order ID from the mapper
$row['increment_id'] = $row['increment_id'] . $this->increment_id_suffix; // add suffix
$row['order_increment_id'] = $row['order_increment_id'] . $this->increment_id_suffix; // add suffix
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row);
$already_obtain_headers = true;
}
$data[] = $row;
}
}
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_invoice_grid ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values) . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING INVOICE GRID FROM PRODUCTION<br /><br />";
}
private function copy_sales_flat_invoice_item($prodlink, $destlink)
{
echo "BEGIN COPYING INVOICE ITEM FROM PRODUCTION<br />";
echo "==========================================================<br /><br />";
if (count($this->invoice_entity_id_mapper) < 1)
return;
$data = array();
$result = mysqli_query($prodlink, "SELECT * FROM sales_flat_invoice_item WHERE parent_id IN ({$this->convertKeysToCommas($this->invoice_entity_id_mapper)}) ORDER BY entity_id");
if (!$result) {
die('Invalid query: ' . mysqli_error($prodlink));
}
$already_obtain_headers = false;
$colHeaders = '';
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$row['parent_id'] = $this->invoice_entity_id_mapper[$row['parent_id']]; // replace the parent ID from the mapper
$row['order_item_id'] = $this->sales_item_id_mapper[$row['order_item_id']]; // replace the order item ID from the mapper
if (!$already_obtain_headers)
{
$colHeaders = $this->writeColHeaders($row, 'sales_flat_invoice_item');
$already_obtain_headers = true;
}
$data[] = $row;
}
}
$already_get_first_item_id = false;
$first_item_id = 0;
// insert into destination table
foreach($data as $key => $values)
{
// insert into destination table
$insertQuery = "INSERT INTO sales_flat_invoice_item ({$colHeaders}) VALUES ";
$insertQuery .= '(' . $this->getColValues($values, 'sales_flat_invoice_item') . ')';
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING INVOICE ITEM FROM PRODUCTION<br /><br />";
}
/* COPY TO TABLE MESSAGE TO BE EXPORTED TO NETSUITE */
private function copy_table_message($destlink)
{
echo "BEGIN COPYING TO TABLE MESSAGE<br />";
echo "==========================================================<br /><br />";
// insert into destination table
// Sales Order first
foreach($this->sales_entity_id_mapper as $key => $value)
{
$queue_id = 1;
$body = 'order_place|' . $value;
$md5 = md5($body);
$created = time();
$priority = 0;
$processing = 0;
// insert into destination table
$insertQuery = "INSERT INTO message (queue_id, body, md5, created, priority, processing) VALUES ";
$insertQuery .= "($queue_id, '$body', '$md5', '$created', $priority, $processing)";
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
// insert into destination table
// Invoice later
foreach($this->invoice_entity_id_mapper as $key => $value)
{
$queue_id = 1;
$body = 'invoice_save|' . $value;
$md5 = md5($body);
$created = time();
$priority = 0;
$processing = 0;
// insert into destination table
$insertQuery = "INSERT INTO message (queue_id, body, md5, created, priority, processing) VALUES ";
$insertQuery .= "($queue_id, '$body', '$md5', '$created', $priority, $processing)";
$result = mysqli_query($destlink, $insertQuery);
if (!$result)
die('Error insert: ' . mysqli_error($destlink));
}
echo "<br />==========================================================<br />";
echo "FINISH COPYING TO TABLE MESSAGE<br /><br />";
}
// get billing address id and shipping address id based on sales order ID
private function get_address_type_ids($order_id, $destlink)
{
$data = array();
$result = mysqli_query($destlink, "SELECT billing_address_id, shipping_address_id FROM sales_flat_order WHERE entity_id = $order_id");
if (!$result) {
die('Invalid query: ' . mysqli_error($destlink));
}
if (count($result) > 0)
{
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
$data['billing_address_id'] = $row['billing_address_id'];
$data['shipping_address_id'] = $row['shipping_address_id'];
}
}
return $data;
}
/* OTHER FUNCTIONS */
private function convertKeysToCommas($data)
{
$result = '';
foreach($data as $key => $value)
$result .= $key . ',';
if (strlen($result) > 0)
$result = substr($result, 0, strlen($result) - 1);
return $result;
}
private function convertValuesToCommas($data)
{
$result = '';
foreach($data as $key => $value)
$result .= $value . ',';
if (strlen($result) > 0)
$result = substr($result, 0, strlen($result) - 1);
return $result;
}
private function writeColHeaders($row, $entity = null)
{
$result = '';
foreach($row as $key => $value)
{
if (!is_null($entity) && in_array($key, $this->fields_excepted[$entity]))
continue;
$result .= $key . ',';
}
if (strlen($result) > 0)
$result = substr($result, 0, strlen($result) - 1);
return $result;
}
private function getColValues($values, $entity = null)
{
$result = '';
foreach($values as $key => $value)
{
if (!is_null($entity) && in_array($key, $this->fields_excepted[$entity]))
continue;
if (is_null($values[$key]))
$result .= 'NULL,';
else
$result .= "'".addslashes($values[$key])."'" . ',';
}
if (strlen($result) > 0)
$result = substr($result, 0, strlen($result) - 1);
return $result;
}
}
?>