eugenegpil / moysklad
来源: https://github.com/Tooyz/moysklad. 修改: https://github.com/EugeneGpil/moysklad
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ^6.2.1
This package is auto-updated.
Last update: 2024-09-12 21:22:08 UTC
README
"Мой склад" JSON API 1.1 的库。一些示例可以在 "tests" 中找到。仍远未完成。
安装
composer require tooyz/moysklad
运行测试
1) composer global require phpunit/phpunit
2) cd tests
3) composer update
4) 编辑 Config.php
5) phpunit --configuration="./phpunit.xml" 或 npm run test
MoySklad 类
用于认证,通常通过参数显式传递到大多数组件,因为可能同时使用多个我的仓库账户。
$sklad = MoySklad::getInstance($login, $password);
实体
库的主要对象
$product = new Product($sklad, [
"name" => "Банан"
]);
例如,可以这样操作
$product = new Product($sklad, [
"id" => "12345-654321-123456-54321",
"name" => "Банан"
]);
$product->fresh(Expand::create(['country']));
或者这样
$product->transformToClass(Counterparty::class);
或者不这样做。大部分逻辑被委派给其他类。
获取实体
获取所有实体
$list = Product::query($sklad)->getList();
可以添加查询参数。参数描述在 QuerySpecs 类中。
$list = Product::query($sklad, QuerySpecs::create([
"offset" => 15,
"maxResults" => 50,
]))->getList();
过滤。FilterQuery 方法的描述在注释中。
$filteredList = Product::query($sklad)->filter(
(new FilterQuery())
->eq("article", 12345)
);
按字符串搜索。可以给 Query 对象附加 expand 以获取指定的名称的关联
$searchedList = Product::query($sklad)->withExpand(Expand::create(['owner']))->search("裤子");
上述函数返回 EntityList 对象。
按 id 获取
$product = Product::query($sklad)->byId("12345-654321-123456-54321");
创建、更新
$counterparty = (new Counterparty($sklad, [
"name" => "Васян"
]))->create();
某些实体在创建时需要指定关联。例如,对于 customerorder,需要指定 counterparty 和 organization,并且可选地指定位置数组
$order = (new CustomerOrder($this->sklad))->buildCreation()
->addCounterparty($counterparty)
->addOrganization($organization)
->addPositionList($positions)
->execute();
更新时也是如此
$product->buildUpdate()->addCountry($country)->execute();
删除
$product->delete();
;
关联
通常通过 API 获取的实体具有某些关联
$product->relations;
知道例如产品有一个关联的 employee,但不知道该字段的名称,可以这样获取它
$employee = $product->relations->find(Employee::class)
因为关联通常以 meta 对象的格式到来,为了获取完整对象,可以这样操作
$group = $product->relations->group->fresh()
如果关联是对象数组,则可以对该数组执行 "获取实体" 部分中描述的操作
$products = $order->relationListQuery("positions")->getList();
实体列表
EntityList - 用于与 API 交互的数组包装器
例如,获取 assortment 并将元素转换为所需类型
$differentProductsAndStuff = Assortment::query($sklad)->getList()->transformItemsToMetaClass();
或者批量创建实体
$neko = new Product($sklad, ["name" => "Кот"]);
$doge = new Product($sklad, ["name" => "Пёс"]);
$el = new EntityList($sklad, [$neko, $doge])->each(function($e) use($vasyan){
$e->buildCreation()->addEmployee($vasyan);
})->massCreate();
可以将其转换为数组
$el->toArray();
;
图片处理
将图片附加到实体
$product->attachImage(ImageField::createFromUrl(
"http://url.ru/img.jpg"
));
或者
$product->attachImage(ImageField::createFromPath(
"images/123.jpg",
"renamed_image.jpg"
));
下载图片
$product->image->download('/usr/images/cat.jpg', 'normal');
/* normal, miniature, tiny - размеры изображений */
Specs
用于配置各种操作的类
<SpecsClass>::create(["field"=>"value"]);
将配置字段传递给 create 方法
LinkingSpecs 用于描述将实体与其他参与更新/创建的第一个实体的关联
具有以下字段
name - 新关联的名称。如果没有指定,将使用我的仓库中的实体名称
multiple - 关联将是实体数组,即可以指定具有相同名称的多个关联
fields - 仅获取创建关联时需要的字段
excludedFields - 获取除了指定的字段之外的所有字段
添加额外字段到合作伙伴的示例
$specs = LinkingSpecs::create([
"name" => "attributes", //в апи доп поля хранятся в поле attributes
"multiple" => true //и являются массивом
]);
$counterparty = $counterparty->buildUpdate()
->addAttribute($attribute, $specs)
->addAttribute($attribute2, $specs)
->execute();
QuerySpecs 配置 EntityQuery 和 RelationQuery 对象
字段
limit - 单次发送请求的结果数量(默认为100)
offset - 结果的偏移量
maxResults - 返回的最大结果数量
expand - 获取指定关系的结果(Expand对象)
updatedFrom - 对象的更新时间小于或等于指定参数值的对象(CommonDate对象)
updatedTo - 对象的更新时间小于或等于指定参数值的对象(CommonDate对象)
updatedBy - 选择所有最后修改者为指定uid的用户的对象。
Product::query($sklad, QuerySpecs::create([
'maxResults' => 25,
'expand' => Expand::create([Employee::$entityName]),
'updatedFrom' => new CommonDate("2017-01-01"),
'updatedBy' => "admin@admin"
]))->getList();
发布
文档实体支持发布
获取
$publications = $customerOrder->getPublications(QuerySpecs::create())
创建
$publication = $customerOrder->createPublication($customTemplate)
删除
$customerOrder->deletePublication($publication)
按id获取发布
$publication = $customerOrder->getPublicationById("123-456")
打印文档
文档实体支持打印
创建
创建打印请求时,可以传递AbstractTemplate或EntityList
$export = $demand->createExport($templateEntity, 'xls');
$exports = $demand->createExport($templateList);
获取标准模板
$templates = $demand->getExportEmbeddedTemplates();
获取用户模板
$templates = $demand->getExportCustomTemplates();
按id获取标准模板
$templates = $demand->getExportEmbeddedTemplateById(123);
按id获取用户模板
$templates = $demand->getExportCustomTemplateById(123);
报告
包含用于获取报告的静态方法。
$report = DashboardReport::day($sklad);
某些方法可以指定特殊的查询请求,例如CounterpartyReportQuerySpecs
$report = SalesReport::byEmployee($sklad, SalesReportQuerySpecs::create([
"counterparty.id" => $cpId
]));
审计
系统事件历史
获取最后5个买家订单的上下文
$audits = Audit::query($this->sklad, QuerySpecs::create([
'maxResults' => 5
]))->filter((new FilterQuery())
->eq("entityType", "customerorder")
);
获取上下文事件
$events = $audit->getAuditEvents();
获取实体事件
$events = $customerOrder->getAuditEvents();
获取过滤器列表
$filters = Audit::getFilters($this->sklad);
调试
静态类RequestLog包含api中的请求/响应历史记录。
可以获取最后一个请求/响应
RequestLog::getLast()
或者所有
RequestLog::getList()
默认情况下,为限制内存消耗,保留50个最后的请求,达到限制后,旧请求将被删除。可以通过以下方式更改限制
RequestLog::setStorageSize(500); // 500 запросов
RequestLog::setStorageSize(0); //Без лимита
其他库
- Ruby https://github.com/dapi/moysklad
- JavaScript/nodejs https://github.com/wmakeev/moysklad-client