22
33namespace App \Controllers \Web ;
44
5+ use App \Models \Cart ;
6+ use App \Models \CartProduct ;
57use App \Models \Order ;
6- use App \Models \Product ;
7- use App \Services \Mail ;
8+ use App \Models \OrderProduct ;
9+ use App \Models \User ;
10+ use App \Models \UserAddress ;
811use Psr \Http \Message \ResponseInterface ;
912use Vesp \Controllers \Controller ;
1013
1114class Orders extends Controller
1215{
16+
17+ public function get (): ResponseInterface
18+ {
19+ $ uuid = $ this ->getProperty ('uuid ' );
20+ /** @var Order $order */
21+ if (!$ uuid || !$ order = Order ::query ()->where ('uuid ' , $ uuid )->first ()) {
22+ return $ this ->failure ('Not Found ' , 404 );
23+ }
24+
25+ return $ this ->success ($ order ->getData ());
26+ }
27+
1328 public function put (): ResponseInterface
1429 {
15- if (!$ ordered = $ this ->getProperty ('products ' )) {
16- return $ this ->failure ();
30+ $ cartId = $ this ->getProperty ('uuid ' );
31+ /** @var Cart $cart */
32+ if (!$ cartId || !$ cart = Cart ::query ()->find ($ cartId )) {
33+ return $ this ->failure ('Not Found ' , 404 );
1734 }
1835
19- $ orderTotal = 0 ;
20- $ orderProducts = [];
21- foreach ($ ordered as $ id => $ amount ) {
22- /** @var Product $product */
23- if ($ product = Product ::query ()->where ('active ' , true )->find ($ id )) {
24- $ total = $ product ->price * $ amount ;
25- $ orderTotal += $ total ;
26- $ orderProducts [] = [
27- 'product_id ' => $ id ,
28- 'title ' => $ product ->title ,
29- 'amount ' => $ amount ,
30- 'price ' => $ product ->price ,
31- 'total ' => $ total ,
32- ];
36+ $ properties = $ this ->getProperties ();
37+ if (!$ user = $ this ->user ) {
38+ try {
39+ $ address = new UserAddress ();
40+ $ address ->fillData ($ properties );
41+
42+ $ properties ['fullname ' ] = $ properties ['receiver ' ] ?? null ;
43+ $ properties ['username ' ] = $ properties ['email ' ] ?? null ;
44+ $ user = User ::createUser ($ properties );
45+ $ address ->user_id = $ user ->id ;
46+ $ address ->save ();
47+ } catch (\Exception $ e ) {
48+ return $ this ->failure ($ e ->getMessage ());
49+ }
50+ } elseif (empty ($ properties ['address_id ' ]) || !$ address = $ user ->addresses ()->find ($ properties ['address_id ' ])) {
51+ try {
52+ $ address = new UserAddress (['user_id ' => $ user ->id ]);
53+ $ address ->fillData ($ properties );
54+ $ address ->save ();
55+ } catch (\Exception $ e ) {
56+ return $ this ->failure ($ e ->getMessage ());
3357 }
3458 }
3559
60+ $ lang = $ this ->request ->getHeaderLine ('Content-Language ' ) ?: 'ru ' ;
61+
3662 $ order = new Order ();
37- $ order ->fill ($ this ->getProperties ());
38- $ order ->total = $ orderTotal ;
39- if ($ order ->save ()) {
40- $ order ->orderProducts ()->createMany ($ orderProducts );
41- }
42- // Use only if STMP is specified
43- if ($ order ->email ) {
44- $ error = (new Mail ())->send (
45- $ order ->email ,
46- 'Спасибо за ваш заказ! ' ,
47- 'email-order-new.tpl ' ,
48- ['data ' => $ order ->toArray ()]
49- );
50- if ($ error ) {
51- return $ this ->failure ($ error );
52- }
63+ $ order ->user_id = $ user ->id ;
64+ $ order ->address_id = $ address ->id ;
65+ $ order ->comment = $ properties ['comment ' ] ?? null ;
66+ $ order ->weight = 0 ;
67+ $ order ->cart = 0 ;
68+ $ order ->discount = 0 ;
69+
70+ $ orderProducts = [];
71+ foreach ($ cart ->cartProducts ()->orderBy ('created_at ' )->cursor () as $ cartProduct ) {
72+ /** @var CartProduct $cartProduct */
73+ $ orderProduct = new OrderProduct ();
74+ $ orderProduct ->product_id = $ cartProduct ->product_id ;
75+ $ orderProduct ->title = $ cartProduct ->product ->getTitle ($ lang );
76+ $ orderProduct ->weight = $ cartProduct ->product ->weight ;
77+ $ orderProduct ->price = $ cartProduct ->product ->price ;
78+ $ orderProduct ->amount = $ cartProduct ->amount ;
79+ $ orderProduct ->discount = 0 ;
80+ $ orderProduct ->options = $ cartProduct ->options ;
81+ $ orderProducts [] = $ orderProduct ;
82+
83+ $ order ->weight += $ cartProduct ->product ->weight * $ cartProduct ->amount ;
84+ $ order ->cart += $ cartProduct ->product ->price * $ cartProduct ->amount ;
5385 }
86+ $ order ->total = $ order ->cart ;
87+
88+ $ order ->save ();
89+ $ order ->orderProducts ()->saveMany ($ orderProducts );
90+
91+ $ order ->sendEmails ($ lang );
5492
55- return $ this ->success ();
93+ return $ this ->success ($ order -> toArray () );
5694 }
5795}
0 commit comments