-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoyskald_add_order.php
102 lines (83 loc) · 2.71 KB
/
moyskald_add_order.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
<?php
error_reporting(E_ALL);
require_once 'moysklad_routine_library.php';
$handle = fopen('php://input', 'r');
$rawData = stream_get_contents($handle);
fclose($handle);
$data = json_decode($rawData, true);
$rawPosition = $data['position'];
$rawCounterparty = $data['counterparty'];
$rawOrganization = $data['organization'];
$counterpartyIdCollection = array_keys($rawCounterparty);
$organizationIdCollection = array_keys($rawOrganization);
$counterpartyId = $counterpartyIdCollection[0];
$organizationId = $organizationIdCollection[0];
$textAddCustomerOrder = '
{
"name": "' . time() . '",
"organization": {
"meta": {
"href": "https://online.moysklad.ru/api/remap/1.1/entity/organization/'
. $organizationId . '",
"type": "organization",
"mediaType": "application/json"
}
},
"agent": {
"meta": {
"href": "https://online.moysklad.ru/api/remap/1.1/entity/counterparty/'
. $counterpartyId . '",
"type": "counterparty",
"mediaType": "application/json"
}
}
}
';
$api = 'https://online.moysklad.ru/api/remap/1.1';
$curl = getCurl();
$curl = setCurl($curl, "$api/entity/customerorder", 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $textAddCustomerOrder);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($textAddCustomerOrder))
);
$isPositionArray = is_array($rawPosition);
$orderPositions= array();
if ($isPositionArray) {
foreach ($rawPosition as $id => $quantity) {
$positionQuantity=floatval($quantity);
$orderPositions[] =
[
"quantity" =>$positionQuantity,
"price"=>0,
"discount"=>0,
"vat"=>0,
"assortment" =>[
"meta"=>[
"href"=>"https://online.moysklad.ru/api/remap/1.1/entity/product/$id",
"type"=>"product",
"mediaType"=>"application/json"
]
],
"reserve"=>$positionQuantity,
];
}
}
$jsonResponse = 'empty';
$isContainPosition = count($orderPositions)>0;
if($isContainPosition ){
$jsonOrderPositions= json_encode($orderPositions);
$customerOrderId = setCustomerOrder($curl);
$curl = getCurl();
$curl = setCurl($curl,
"$api/entity/customerorder/"
. "$customerOrderId/positions",
'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonOrderPositions);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonOrderPositions))
);
$jsonResponse = setCustomerOrderPosition($curl);
}
var_export($jsonResponse);