rar/yii2-moysklad

用于通过JSON API 1.2操作“我的仓库”的扩展

安装: 254

依赖: 0

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 1

开放问题: 0

类型:yii2-extension

dev-master 2021-03-12 08:52 UTC

This package is not auto-updated.

Last update: 2024-09-26 20:38:19 UTC


README

为“我的仓库”JSON API 1.2提供服务的库。

安装

php composer.phar require --prefer-dist rare/yii2-moysklad "*"
or add
"rare/yii2-moysklad": "*"

将以下内容添加到您的composer.json文件的require部分。

MoySklad类

用于Token授权。可以从“我的仓库”控制台获取它。

$sklad = MoySklad::getInstance($token);

实体

库的主要对象

$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)
);

按字符串搜索。可以将expand附加到Query对象以获取指定名称的关联

$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 - 仅包含由参数中指定的 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); //Без лимита

其他库