skynet/moysklad-module

克隆自: https://github.com/Tooyz/moysklad

0.4.20 2020-11-05 22:47 UTC

README

SensioLabsInsight

为“我的仓库”服务提供的JSON API 1.1库。一些示例可以在“tests”中找到。仍远未完成。

安装

composer require tooyz/moysklad

运行测试

1) composer global require phpunit/phpunit<br />
2) cd tests<br />
3) composer update<br />
4) Отредактировать Config.php <br />
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 - 获取除指定字段之外的所有字段

例如,向counterparty添加额外字段

$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 - 选择所有最后修改者用户 ID 与参数值中指定的 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); //Без лимита

要停止日志记录,可以调用

RequestLog::setEnabled(false);

其他库