/
cart.php
2799 lines (2361 loc) · 112 KB
/
cart.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/******************************************************************
* Copyright 2011 Ktools.net LLC - All Rights Reserved
* http://www.ktools.net
* Created: 4-21-2011
* Modified: 4-21-2011
******************************************************************/
define('BASE_PATH',dirname(__FILE__)); // Define the base path
define('PAGE_ID','cart'); // Page ID
define('ACCESS','public'); // Page access type - public|private
define('INIT_SMARTY',true); // Use smarty
require_once BASE_PATH.'/assets/includes/session.php';
require_once BASE_PATH.'/assets/includes/initialize.php';
require_once BASE_PATH.'/assets/includes/commands.php';
require_once BASE_PATH.'/assets/includes/init.member.php';
require_once BASE_PATH.'/assets/includes/security.inc.php';
require_once BASE_PATH.'/assets/includes/language.inc.php';
require_once BASE_PATH.'/assets/includes/cart.inc.php';
require_once BASE_PATH.'/assets/includes/affiliate.inc.php';
//define('META_TITLE',''); // Override page title, description, keywords and page encoding here
//define('META_DESCRIPTION','');
//define('META_KEYWORDS','');
//define('PAGE_ENCODING','');
define('META_TITLE',$lang['cart'].' – '.$config['settings']['site_title']); // Assign proper meta titles
require_once BASE_PATH.'/assets/includes/header.inc.php';
require_once BASE_PATH.'/assets/includes/errors.php';
require_once BASE_PATH.'/assets/classes/mediatools.php';
//echo $_COOKIE['cart']['uniqueOrderID']; exit;
if(!$_SESSION['uniqueOrderID']) // Create a unique order ID if one isn't already created
{
// Check cookie
if($_COOKIE['cart']['uniqueOrderID'])
$_SESSION['uniqueOrderID'] = $_COOKIE['cart']['uniqueOrderID'];
else
{
$newUniqueOrderID = create_unique2();
$_SESSION['uniqueOrderID'] = $newUniqueOrderID;
// Set Cookie
if($config['useCookies']) setcookie("cart[uniqueOrderID]", $newUniqueOrderID, time()+60*60*24*30, "/", $host[0]); // Set a cart id cookie
}
}
// Check if this unique order id is already in the db with a status other than incomplete
$orderCheckResult = mysqli_query($db,"SELECT SQL_CALC_FOUND_ROWS uorder_id FROM {$dbinfo[pre]}orders WHERE uorder_id = '{$_SESSION[uniqueOrderID]}' AND order_status != '2'");
if(getRows())
$_SESSION['uniqueOrderID'] = create_unique2(); // The unique order id is already in the db - create a new one
if(!$_SESSION['uniqueOrderID']) // Make sure an order ID was created and if not die
die("No order ID was ever created");
if(!$miniCart)
unset($_SESSION['currentMode']); // Unset the gallery mode
//print_k($_SESSION['cartTotalsSession']); exit; // Testing
try
{
if($config['EncryptIDs']) // Decrypt IDs
{
$id = k_decrypt($id);
if($mediaID)
{
$mediaID = k_decrypt($mediaID);
idCheck($mediaID); // Make sure ID is numeric
}
if($profileID)
{
$profileID = k_decrypt($profileID);
idCheck($profileID); // Make sure ID is numeric
}
}
/*
* Update the pay type on a cart item
*/
if($cartMode == 'updatePayType')
{
$cid = k_decrypt($cid);
mysqli_query($db,"UPDATE {$dbinfo[pre]}invoice_items SET paytype='{$payType}' WHERE oi_id = '{$cid}'");
@mysqli_query($db,"UPDATE {$dbinfo[pre]}commission SET comtype='{$payType}' WHERE oitem_id = '{$cid}'"); // Update the pay type in the commissions table
}
/*
* Apply a coupon code
*/
if($cartMode == 'applyCouponCode')
{
$couponCode = strtoupper($couponCode); // Make sure coupon code is uppercase
$promotionsResult = mysqli_query($db,
"
SELECT SQL_CALC_FOUND_ROWS *
FROM {$dbinfo[pre]}promotions
LEFT JOIN {$dbinfo[pre]}perms
ON ({$dbinfo[pre]}promotions.promo_id = {$dbinfo[pre]}perms.item_id AND {$dbinfo[pre]}perms.perm_area = 'promotions')
WHERE {$dbinfo[pre]}promotions.active = 1
AND {$dbinfo[pre]}promotions.deleted = 0
AND ({$dbinfo[pre]}promotions.everyone = 1 OR {$dbinfo[pre]}perms.perm_value IN ({$memberPermissionsForDB}))
AND ({$dbinfo[pre]}promotions.quantity = '' OR {$dbinfo[pre]}promotions.quantity > '0')
AND {$dbinfo[pre]}promotions.promo_code = '{$couponCode}'
"
);
if($returnRows = getRows()) //$returnRows = mysqli_num_rows($promotionsResult)
{
// Remove previous coupon first
unset($_SESSION['cartCouponsArray']);
$promotions = mysqli_fetch_array($promotionsResult);
if(!$_SESSION['cartCouponsArray'][$promotions['promo_id']]) // Make sure coupon wasn't already added
{
if($promotions['quantity'] > 0) // Make sure there are some remaining
{
if($promotions['oneuse'] and !$_SESSION['loggedIn'])
$messageArray[] = 'couponNeedsLogin'; // Need to login warning
else
{
$memberCouponUsageRows = 0; // No usage
if($promotions['oneuse'])
{
// Check to make sure member did not already use this coupon
$memberCouponUsageResult = mysqli_query($db,"SELECT * FROM {$dbinfo[pre]}usedcoupons WHERE mem_id = '{$_SESSION[member][mem_id]}' AND promo_id = '{$promotions[promo_id]}'");
$memberCouponUsageRows = mysqli_num_rows($memberCouponUsageResult);
}
if($memberCouponUsageRows == 0) // User is ok to use this coupon
{
if($promotions['minpurchase'] and $promotions['minpurchase'] > $_SESSION['cartTotalsSession']['priceSubTotal'] and $promotions['promotype'] != 'bulk') // Check if the minimum is met
{
$messageArray[] = 'couponMinumumWarn'; // Not enough total warning
}
else
{
$_SESSION['cartCouponsArray'][$promotions['promo_id']] = promotionsList($promotions);
$messageArray[] = 'couponApplied';
}
}
else
$messageArray[] = 'couponAlreadyUsed'; // Already used coupon warning
}
}
else
$messageArray[] = 'couponFailed'; // All used up
}
}
else
{
$messageArray[] = 'couponFailed';
}
$smarty->assign('message',$messageArray); // Assign the coupon status to smarty
}
/*
* Remove coupon
*/
if($cartMode == 'removeCoupon')
{
unset($_SESSION['cartCouponsArray'][$couponID]);
}
/*
* Update cart quantities
*/
if($cartMode == 'updateQuantities')
{
if($quantity)
{
foreach($quantity as $quantityCartID => $quantityCount)
{
if($quantityCount > 0)
{
// [todo] check to make sure if there are enough available first
// [todo] should we update price_total here???
// Update the price_totals
$cartItemResult = mysqli_query($db,"SELECT price,paytype,credits FROM {$dbinfo[pre]}invoice_items WHERE oi_id = '{$quantityCartID}'");
$cartItem = mysqli_fetch_assoc($cartItemResult);
$newPriceTotal = $cartItem['price']*$quantityCount;
$newCreditTotal = $cartItem['credits']*$quantityCount;
/*
if($cartItem['paytype'] == 'cur')
$newPriceTotal = $cartItem['price']*$quantityCount;
else
$newCreditTotal = $cartItem['credits']*$quantityCount;
*/
//mysqli_query($db,"UPDATE {$dbinfo[pre]}invoice_items SET quantity='{$quantityCount}' WHERE oi_id = '{$quantityCartID}'");
mysqli_query($db,"UPDATE {$dbinfo[pre]}invoice_items SET quantity='{$quantityCount}',price_total='{$newPriceTotal}',credits_total='{$newCreditTotal}' WHERE oi_id = '{$quantityCartID}'");
@mysqli_query($db,"UPDATE {$dbinfo[pre]}commission SET item_qty='{$quantityCount}' WHERE oitem_id = '{$quantityCartID}'"); // Update the quantity in the commissions table
}
}
}
}
/*
* Select cart from database
*/
$cartResult = mysqli_query($db,
"
SELECT SQL_CALC_FOUND_ROWS * FROM {$dbinfo[pre]}orders
LEFT JOIN {$dbinfo[pre]}invoices
ON {$dbinfo[pre]}orders.order_id = {$dbinfo[pre]}invoices.order_id
WHERE {$dbinfo[pre]}orders.uorder_id = '{$_SESSION[uniqueOrderID]}'
"
);
//$cartRows = mysqli_num_rows($cartResult);
if($cartRows = getRows())
{
$cart = mysqli_fetch_array($cartResult); // Select cart details
$cartID = $cart['order_id'];
$invoiceID = $cart['invoice_id'];
}
else
{
if(mysqli_query($db,"INSERT INTO {$dbinfo[pre]}orders (uorder_id,order_status) VALUES ('{$_SESSION[uniqueOrderID]}','2')")) // No cart exists - create one
{
$cartID = mysqli_insert_id($db);
mysqli_query($db,"INSERT INTO {$dbinfo[pre]}invoices (order_id) VALUES ('{$cartID}')"); // Create an invoice
$invoiceID = mysqli_insert_id($db);
}
else
die("Creating a cart entry in the DB failed"); // Fail if a cart/order cannot be created in the db
}
/*
* Remove an item from the cart
*/
if($mode == 'remove' or $edit)
{
if($edit) $cid = $edit; // Set the cart ID from the edit field
$cid = k_decrypt($cid);// Cart item ID
$removeItemResult = mysqli_query($db,
"
SELECT SQL_CALC_FOUND_ROWS oi_id,item_type FROM {$dbinfo[pre]}invoice_items
WHERE oi_id = '{$cid}'
"
); // First grab the item from the invoice items db
if($removeItemRows = getRows()) // $removeItemRows = mysqli_num_rows($removeItemResult)
{
$removeItem = mysqli_fetch_array($removeItemResult);
$deleteArray[] = $removeItem['oi_id']; // Add id to the delete array
if($removeItem['item_type'] == 'package') // Check if this is a package
{
$removePackageItemResult = mysqli_query($db,
"
SELECT oi_id,pack_invoice_id FROM {$dbinfo[pre]}invoice_items
WHERE pack_invoice_id = '{$removeItem[oi_id]}'
"
); // Select package items and add them to the array
while($removePackageItem = mysqli_fetch_array($removePackageItemResult))
$deleteArray[] = $removePackageItem['oi_id']; // Add id to the delete array
}
$deleteArrayFlat = implode(",",$deleteArray);
mysqli_query($db,"DELETE FROM {$dbinfo[pre]}invoice_items WHERE oi_id IN ($deleteArrayFlat)"); // Delete invoice items
mysqli_query($db,"DELETE FROM {$dbinfo[pre]}invoice_options WHERE invoice_item_id IN ($deleteArrayFlat)"); // Delete invoice options
@mysqli_query($db,"DELETE FROM {$dbinfo[pre]}commission WHERE oitem_id IN ($deleteArrayFlat)"); // Delete commission entry
}
//print_r($deleteArray); exit;
//mysqli_query($db,"UPDATE {$dbinfo[pre]}invoice_items SET deleted='1' WHERE oi_id = '{$cid}'"); // Mark the invoice item as deleted in the DB
unset($_SESSION['packagesInCartSession'][$cid]); // Remove this package from the session
}
/*
* Add an item to the cart
*/
if($mode == 'add')
{
if(preg_match("/[^A-Za-z0-9_-]/",$type))
{
header("location: error.php?eType=invalidQuery");
exit;
}
if($option){ foreach($option as $optionValue){ if($optionValue) $optionsPassed = 1; } } // Check to see if any options were passed
if($optionsPassed or $type == 'package')
$hasOptions = 1;
else
$hasOptions = 0;
if($mediaID) // Has a media ID check the owner
{
$mediaOwnerResult = mysqli_query($db,"SELECT owner FROM {$dbinfo[pre]}media WHERE media_id = '{$mediaID}'");
$mediaOwner = mysqli_fetch_assoc($mediaOwnerResult);
$owner = $mediaOwner['owner'];
if($owner) // There is a contributor on the purchase - find commission and make entry into commissions table
{
if($owner != $_SESSION['member']['mem_id']) // Make sure the owner is not the current member
{
$commission['mediaID'] = $mediaID;
$commission['owner'] = $owner;
// Select member account
$contrObj = new memberTools($owner);
$contr = $contrObj->getMemberInfoFromDB($owner);
if($contr) // Make sure member exists
{
$contrMembership = $contrObj->getMembershipInfoFromDB($contr['membership']); // Select membership
if($contrMembership['allow_selling']) // Check if membership allows selling
{
$commission['status'] = true; // Member can receive commission
if($contr['com_source'] == 1) // Use membership comlevel
$commission['memPercent'] = $contrMembership['commission'];
else
$commission['memPercent'] = $contr['com_level'];
}
}
//print_r($member); // testing
//print_r($membership);
//exit;
//echo $memberCommissionLevel; exit;
}
}
}
if($type == 'package')
{
$packageTotalMediaNeeded = count($packageItemMedia);
$packageTotalMediaFilled = 0;
if($packageItemMedia)
{
foreach($packageItemMedia as $packageItemMediaValue)
{
if($packageItemMediaValue)
$packageTotalMediaFilled++;
}
}
}
switch($type) // Set only digital to a non physical item and make everything else physical
{
case 'digital':
case 'collection':
$physicalItem = 0;
break;
default:
$physicalItem = 1;
break;
}
$defaultPayType = (currencyCartStatus()) ? 'cur' : 'cred'; // Find the default pay type that this item should use
$commission['comtype'] = $defaultPayType; // Set the correct paytype for commissions
if($type == 'credits') $defaultPayType = 'cur'; // Force credits to use currency type
mysqli_query($db,
"
INSERT INTO {$dbinfo[pre]}invoice_items
(
invoice_id,
item_type,
item_id,
asset_id,
quantity,
physical_item,
has_options,
item_added,
package_media_needed,
package_media_filled,
paytype,
contributor_id
)
VALUES
(
'{$invoiceID}',
'{$type}',
'{$id}',
'{$mediaID}',
'1',
'{$physicalItem}',
'{$hasOptions}',
'{$nowGMT}',
'{$packageTotalMediaNeeded}',
'{$packageTotalMediaFilled}',
'{$defaultPayType}',
'{$owner}'
)
"
); // Insert the item into the DB
$cartItemID = mysqli_insert_id($db);
if($type == 'package') // If this is a package input all package items into the invoice items table
{
$packageResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}packages
WHERE pack_id = '{$id}'
"
); // Select item pricing from the db
$package = mysqli_fetch_array($packageResult);
$package['price'] = defaultPrice($package['price']); // Make sure to assign a default price if needed
$package['credits'] = defaultCredits($package['credits']); // Make sure to assign default credits if needed
$itemPrice = $package['price'];
$itemCredits = $package['credits'];
$optionsPrice = 0;
$optionsCredits = 0;
if($_POST['packageItem'])
{
foreach($_POST['packageItem'] as $key => $value)
{
$packageItemDetailsArray = explode("-",$value);
/*
echo "Item ID: {$packageItemDetailsArray[1]}<br />";
echo "Item Type: {$packageItemDetailsArray[0]}<br />";
echo "Key: {$key}<br />";
echo "Quantity: {$_POST[quantity][$key]}<br /><br />";
*/
if($packageItemDetailsArray[0] == 'collection') // See if this is a physical item or not
$packageItemPhysical = 0;
else
$packageItemPhysical = 1;
mysqli_query($db,
"
INSERT INTO {$dbinfo[pre]}invoice_items
(
invoice_id,
item_type,
item_id,
asset_id,
quantity,
physical_item,
pack_invoice_id,
item_added,
item_list_number
)
VALUES
(
'{$invoiceID}',
'{$packageItemDetailsArray[0]}',
'{$packageItemDetailsArray[1]}',
'{$packageItemMedia[$key]}',
'{$_POST[packageItemQuantity][$key]}',
'{$packageItemPhysical}',
'{$cartItemID}',
'{$nowGMT}',
'{$key}'
)
"
);
$packageCartItemID = mysqli_insert_id($db);
/*
* Save any options that were selected
*/
if(${'option'.$key})
{
foreach(${'option'.$key} as $optionKey => $optionValue)
{
if($optionValue)
{
$optionKeyArray = explode("-",$optionKey);
if($optionKeyArray[1])
$opSelection = $optionKeyArray[1];
else
$opSelection = $optionValue;
// Get option price
$optionResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}options
WHERE op_id = '{$opSelection}'
"
); // Select item pricing from the db
$option = mysqli_fetch_array($optionResult);
switch($option['price_mod'])
{
case "add":
$optionsPrice+=$option['price'];
break;
case "sub":
$optionsPrice-=$option['price'];
break;
}
switch($option['credits_mod'])
{
case "add":
$optionsCredits+=$option['credits'];
break;
case "sub":
$optionsCredits-=$option['credits'];
break;
}
mysqli_query($db,
"
INSERT INTO {$dbinfo[pre]}invoice_options
(
option_gid,
item_list_number,
option_id,
invoice_item_id,
option_price,
option_price_calc,
option_credits,
option_credits_calc
)
VALUES
(
'{$optionKeyArray[0]}',
'{$key}',
'{$opSelection}',
'$packageCartItemID',
'{$option[price]}',
'{$option[price_mod]}',
'{$option[credits]}',
'{$option[credits_mod]}'
)
"
); // Insert the options selected into the cart
}
}
}
}
}
$_SESSION['packagesInCartSession'][$cartItemID] = $id; // Add an item to the packages in cart session
$itemPrice+= $optionsPrice; // Price with options
$itemCredits+= $optionsCredits; // Credits with options
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
}
else
{
switch($type)
{
/*
* Get the subscription price and update the invoice item
*/
case "subscription":
$addItemResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}subscriptions
WHERE sub_id = '{$id}'
"
); // Select item pricing from the db
$addItem = mysqli_fetch_array($addItemResult);
$itemPrice = defaultPrice($addItem['price']); // Get the price of this item
$itemCredits = defaultCredits($addItem['credits']); // Get the credit value of this item
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
break;
/*
* Get the credits price and update the invoice item
*/
case "credits":
$addItemResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}credits
WHERE credit_id = '{$id}'
"
); // Select item pricing from the db
$addItem = mysqli_fetch_array($addItemResult);
$itemPrice = defaultPrice($addItem['price']); // Get the price of this item
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
break;
/*
* Get the collection price and update the invoice item
*/
case "collection":
$addItemResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}collections
WHERE coll_id = '{$id}'
"
); // Select item pricing from the db
$addItem = mysqli_fetch_array($addItemResult);
$itemPrice = defaultPrice($addItem['price']); // Get the price of this item
$itemCredits = defaultCredits($addItem['credits']); // Get the credit value of this item
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
break;
case "print":
// select the media details
$sql = "SELECT * FROM {$dbinfo[pre]}media WHERE media_id = '{$mediaID}'";
$mediaInfo = new mediaList($sql);
$media = $mediaInfo->getSingleMediaDetails('thumb');
$mediaPrice = getMediaPrice($media); // Get the media price based on the license
$mediaCredits = getMediaCredits($media); // Get the media credits based on the license
$printResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}prints
WHERE print_id = '{$id}'
"
); // Select item pricing from the db
$print = mysqli_fetch_array($printResult);
$print['price'] = defaultPrice($print['price']); // Make sure to assign a default price if needed
$print['credits'] = defaultCredits($print['credits']); // Make sure to assign default credits if needed
/*
* Custom Pricing calculations
*/
$mediaPrintsResult = mysqli_query($db,"SELECT * FROM {$dbinfo[pre]}media_prints WHERE media_id = '{$mediaID}' AND print_id = '{$id}'"); // Find if this has a customization
if(mysqli_num_rows($mediaPrintsResult))
{
$mediaPrint = mysqli_fetch_array($mediaPrintsResult);
if($mediaPrint['customized']) // See if this entry was customized
{
$print['price_calc'] = $mediaPrint['price_calc'];
$print['price'] = defaultPrice($mediaPrint['price']);
$print['credits'] = defaultCredits($mediaPrint['credits']);
$print['credits_calc'] = $mediaPrint['credits_calc'];
$print['quantity'] = $mediaPrint['quantity'];
}
}
/*
* Advanced Pricing calculations
*/
switch($print['price_calc'])
{
case 'add':
$print['price'] = $mediaPrice + $print['price'];
break;
case 'sub':
$print['price'] = $mediaPrice - $print['price'];
break;
case 'mult':
$print['price'] = $mediaPrice * $print['price'];
break;
}
switch($print['credits_calc'])
{
case 'add':
$print['credits'] = $mediaCredits + $print['credits'];
break;
case 'sub':
$print['credits'] = $mediaCredits - $print['credits'];
break;
case 'mult':
$print['credits'] = $mediaCredits * $print['credits'];
break;
}
$itemPrice = $print['price'];
$itemCredits = $print['credits'];
$optionsPrice = 0;
$optionsCredits = 0;
if($option)
{
// Get the price of all options - Save options
foreach($option as $optionKey => $optionValue)
{
if($optionValue)
{
$optionKeyArray = explode("-",$optionKey);
if($optionKeyArray[1])
$opSelection = $optionKeyArray[1];
else
$opSelection = $optionValue;
// Get option price
$optionResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}options
WHERE op_id = '{$opSelection}'
"
); // Select item pricing from the db
$option = mysqli_fetch_array($optionResult);
switch($option['price_mod'])
{
case "add":
$optionsPrice+=$option['price'];
break;
case "sub":
$optionsPrice-=$option['price'];
break;
}
switch($option['credits_mod'])
{
case "add":
$optionsCredits+=$option['credits'];
break;
case "sub":
$optionsCredits-=$option['credits'];
break;
}
mysqli_query($db,
"
INSERT INTO {$dbinfo[pre]}invoice_options
(
option_gid,
item_list_number,
option_id,
invoice_item_id,
option_price,
option_price_calc,
option_credits,
option_credits_calc
)
VALUES
(
'{$optionKeyArray[0]}',
'{$key}',
'{$opSelection}',
'{$cartItemID}',
'{$option[price]}',
'{$option[price_mod]}',
'{$option[credits]}',
'{$option[credits_mod]}'
)
"
); // Insert the options selected into the cart
}
}
}
$itemPrice+= $optionsPrice; // Price with options
$itemCredits+= $optionsCredits; // Credits with options
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
if($owner) // Add commission record
{
$commission['oitemID'] = $cartItemID;
$commission['comTotal'] = $itemPrice;
$commission['comCredits'] = $itemCredits;
if($print['commission_type'] == 1) // 1 = Percentage | 2 = Dollar Value
{
$commission['itemPercent'] = $print['commission']; //com_percentage
$commission['comTotal'] = $itemPrice;
}
else
{
$commission['itemPercent'] = 100; // Item commission is 100% of dollar value
$commission['comTotal'] = $print['commission_dollar'];
}
//print_k($commission); exit;
if($owner != $_SESSION['member']['mem_id']) addCommissionRecord($commission); // Make sure the owner is not the current member
}
break;
case "product":
// select the media details
if($mediaID)
{
$sql = "SELECT * FROM {$dbinfo[pre]}media WHERE media_id = '{$mediaID}'";
$mediaInfo = new mediaList($sql);
$media = $mediaInfo->getSingleMediaDetails('thumb');
$mediaPrice = getMediaPrice($media); // Get the media price based on the license
$mediaCredits = getMediaCredits($media); // Get the media credits based on the license
}
$productResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}products
WHERE prod_id = '{$id}'
"
); // Select item pricing from the db
$product = mysqli_fetch_array($productResult);
$product['price'] = defaultPrice($product['price']); // Make sure to assign a default price if needed
$product['credits'] = defaultCredits($product['credits']); // Make sure to assign default credits if needed
if($product['product_type'] == '1') // Only do the following if this is a media based product
{
/*
* Custom Pricing calculations
*/
$mediaProductsResult = mysqli_query($db,"SELECT * FROM {$dbinfo[pre]}media_products WHERE media_id = '{$mediaID}' AND prod_id = '{$id}'"); // Find if this has a customization
if(mysqli_num_rows($mediaProductsResult))
{
$mediaProduct = mysqli_fetch_array($mediaProductsResult);
if($mediaProduct['customized']) // See if this entry was customized
{
$product['price_calc'] = $mediaProduct['price_calc'];
$product['price'] = defaultPrice($mediaProduct['price']);
$product['credits'] = defaultCredits($mediaProduct['credits']);
$product['credits_calc'] = $mediaProduct['credits_calc'];
$product['quantity'] = $mediaProduct['quantity'];
}
}
/*
* Advanced Pricing calculations
*/
switch($product['price_calc'])
{
case 'add':
$product['price'] = $mediaPrice + $product['price'];
break;
case 'sub':
$product['price'] = $mediaPrice - $product['price'];
break;
case 'mult':
$product['price'] = $mediaPrice * $product['price'];
break;
}
switch($product['credits_calc'])
{
case 'add':
$product['credits'] = $mediaCredits + $product['credits'];
break;
case 'sub':
$product['credits'] = $mediaCredits - $product['credits'];
break;
case 'mult':
$product['credits'] = $mediaCredits * $product['credits'];
break;
}
}
$itemPrice = $product['price'];
$itemCredits = $product['credits'];
$optionsPrice = 0;
$optionsCredits = 0;
// Get the price of all options - Save options
if($option)
{
foreach($option as $optionKey => $optionValue)
{
if($optionValue)
{
$optionKeyArray = explode("-",$optionKey);
if($optionKeyArray[1])
$opSelection = $optionKeyArray[1];
else
$opSelection = $optionValue;
// Get option price
$optionResult = mysqli_query($db,
"
SELECT * FROM {$dbinfo[pre]}options
WHERE op_id = '{$opSelection}'
"
); // Select item pricing from the db
$option = mysqli_fetch_array($optionResult);
switch($option['price_mod'])
{
case "add":
$optionsPrice+=$option['price'];
break;
case "sub":
$optionsPrice-=$option['price'];
break;
}
switch($option['credits_mod'])
{
case "add":
$optionsCredits+=$option['credits'];
break;
case "sub":
$optionsCredits-=$option['credits'];
break;
}
mysqli_query($db,
"
INSERT INTO {$dbinfo[pre]}invoice_options
(
option_gid,
item_list_number,
option_id,
invoice_item_id,
option_price,
option_price_calc,
option_credits,
option_credits_calc
)
VALUES
(
'{$optionKeyArray[0]}',
'{$key}',
'{$opSelection}',
'{$cartItemID}',
'{$option[price]}',
'{$option[price_mod]}',
'{$option[credits]}',
'{$option[credits_mod]}'
)
"
); // Insert the options selected into the cart
}
}
}
$itemPrice+= $optionsPrice; // Price with options
$itemCredits+= $optionsCredits; // Credits with options
mysqli_query($db,
"
UPDATE {$dbinfo[pre]}invoice_items SET
price='{$itemPrice}',
price_total='{$itemPrice}',
credits='{$itemCredits}',
credits_total='{$itemCredits}'
WHERE oi_id = '{$cartItemID}'
"
); // Update the invoice item with the prices
if($owner) // Add commission record
{
$commission['oitemID'] = $cartItemID;
$commission['comTotal'] = $itemPrice;
$commission['comCredits'] = $itemCredits;