📜 ⬆️ ⬇️

Easy integration of the site and 1C

image
Recently, I came across several different articles about the integration of the site and 1C. In the comments often began disputes about different approaches, and I decided to share the way that once happened to realize me. Of course, the method described below does not pretend to universality and uniqueness, but I think it will be useful to those who are just going to write their own version.


Choppers, rakes and shovels


Not a very big store (nomenclature of about 5000 products).
Ordinary, file 1C 8 UT. The configuration is fully supported and cannot be removed from support.
The base lies on one of the desktop machines, which in turn is used to work as one of the employees.
It is enough to upload a minimum of information: name, description, photo, several types of prices.
Breaking an already established ecosystem was also extremely undesirable.
Under the site is allocated an ordinary virtual hosting, which means that you need to take into account the limited resources of such.
Unloading can occur not often, it is enough 1 time in 3 hours.
Subsequently, it became necessary to add unloading of the price list in xls.
Modest budget for the project.

A machine that performs the role of a “server” was already not fast, and the presence of permanent connections to the 1C database only aggravated the situation. But it suited the customer, which means there were no heaps of money for the purchase of a separate server and the re-equipment of the rest of the park.
Due to budget constraints, the variant with Bitrix and its type integration disappeared immediately. Yes, and the interest was more sporty, to implement everything yourself. It was decided to use the framework previously used for the product catalog. The framework was made on CodeIgniter, so adding a small module of labor was not. Solved.
')

Let's start


The first thing was the question of the frequency of information upload, and the hands reached for the routine tasks ... but stop. First, the configuration cannot be removed from support, which means that we cannot edit the configuration itself, and secondly, all copies of 1C are launched only when necessary, which means whether 1C will be launched at the right time or not is unknown . Yes, it would be possible to oblige the customer, when starting the "server", always run 1C and always keep it running, but this will create unnecessary inconvenience to the customer, which means the solution is not the most convenient. Alas, routine tasks today will not be able to help us. Obviously, we need the help of a third-party application to start the upload process at a certain moment. Here we recall the fact that 1C allows you to run yourself from the command line, moreover, we can immediately perform the external processing we need and, if necessary, there is even the possibility to pass some parameter to it.

Here are the main keys that will be used:
"  1" enterprise /F"  " /N"" /P"" /Execute" " /C"" /DisableStartupMessages 


Now it remains to configure the launch of this design on a schedule. Windows Scheduler? Customize, check ... works!
But there is one serious flaw. When the scheduler launches a 1C schedule, then, of course, it opens above all applications, and if someone is working on a computer at that time, then, firstly, it will distract him, and secondly, he may close it new window. Not order ... What to do? We start to dig in the direction of launch under a different account. Thanks to Google, we quickly find the possibility of batch launching under another user. Create a new Windows user, enable batch launch in security policies, reconfigure the scheduler, check ... voila! Everything worked, but we did not see any annoying windows! Well, it means that this option suits us ... now we turn to the actual unloading of the data itself.

Unloading


First of all, of course, I began to look towards CommerceML, but after reading the documentation, it became clear that for our rather basic unloading it was too long to fence the whole garden, and the budget was not rubber. So, we will look for an alternative way. Why not just upload text information in xml, and upload images to a separate directory? Resolved, so doing. We get a fairly simple xml-file structure: inside, first go records for groups, and then nomenclature units themselves.

This is how the groups look:
  <group> <Code></Code> /*-  1*/ <Name></Name> /* ,  */ <ParentCode> </ParentCode> /*   ,    */ </group> 


And so the goods:
  <item> <Code></Code> /* -  1 */ <ParentCode> </ParentCode> /*  ,     */ <Name></Name> <Descr></Descr> <Article></Article> <TypePrice> </TypePrice> <Price></Price> <urrency></urrency> <Remains></Remains> <Unit>//</Unit> <Img>img_dae5eacd-7d88-11de-8856-0024213f1c89.jpg</Img> /*           1 */ </item> 


Not a tricky request, we receive information on all product groups and products, then we form an xml-ku. We also make a separate sample and pull out the information for the price list, and then store the received information in xls.
After that, go to upload images. Due to the limitations of the hosting site, it seemed not the most rational to process images on the hosting side, so it was decided to do image processing + creating previews at the stage of uploading. 1C is of course a combine, but I did not find an opportunity to process a pack of photos. But do we have ImageMagick? An excellent, cross-platform set of everything we need for image processing ... download, unpack ... it remains only to write bat'nik, which will perform the necessary manipulations:

 cd c:\ set thePATH=      FOR /R "%thePATH%" %%a IN (*.jpg) DO ImageMagick-6.7.0-10\convert.exe %%a -resize x^> -quality 70 %%a FOR /R "%thePATH%" %%a IN (*.jpg) DO ImageMagick-6.7.0-10\convert.exe %%a -resize x^> -gravity center -extent x -quality 70 %thePATH%mini\%%~nxa 


We start this bat'nik from our processing and get the result.

All information is unloaded, it remains to pack everything neatly into zip and upload via FTP to the server, which 1C does well with standard tools.

At first glance, everything, but ... Are we going to pull out the entire nomenclature every time, with all the pictures, and load this whole machine into the server? No, this picture is not for the faint of heart ... And what to do? Make flags and mark which item has changed? No, we can not edit the configuration ... But there are plans for the exchange? Who forbade us to use them? No one! Create an exchange plan, customize. Next, we modify our upload, now during the upload we create a new message in terms of exchange, upload only the units that have been changed since the last message ... Excellent!

Server part


It's still more transparent here. We hang up the launch of the script on the crown. In the script we look in the right directory, we search for the required archive by mask. If there is one, then unpack, parse xml, write information to the database. We have pictures inside with another archive, we also unpack it, right on top of the directory with the existing pictures. If such a picture already exists, rewrite it with a new one. Copy xls-file with the replacement in the desired directory. We write to the database information about what made such an update, then, with such a number, and then clean the temporary files. Everything.

Straw from failures


If in the process of uploading information, image processing, archiving, uploading to a remote FTP, errors occur, we may lose some updates, and as a result, the information on the site and in the 1C base will be out of sync. Below I described the main sharp corners for which it was necessary to lay straws:

1. So, what will happen if, for example, at the moment of downloading via FTP, the connection is broken?
If we have already recorded a message in the exchange plan, it turns out that 1C believes that it has successfully unloaded these changes and is now collecting new ones, but they did not get to the site! That's right, so we end up writing to the exchange plan only after successfully completing all the upload steps, including uploading files via FTP! And if in the process of uploading an exception occurs, then we interrupt the recording of the message in terms of the exchange and write the error in the error-log.

2. And what if our script on the server starts processing the not yet uploaded archive?
We first give it one name (for example, export.zip_), but only upon completion we rename it and give it a name that will search for the script on the server.

3. And what if the server fails and the script does not have time to process our archive, 1C will wipe it with a new one?
No, for this, each archive contains in the title a message number from the exchange plan (for example, export_1.zip). The script on the server in turn, when it detects several archives, processes them in ascending order of numbers.

4. And the logs are not overcrowded?
Because when uploading, information about the results of each action is written to the log in detail, then the logs grow quite quickly, so we don’t forget to check their size with each upload and, if necessary, delete the old ones.

5. And what if 1C does not have time to perform the unloading, and at this time the process starts for a new unloading?
In the settings of the scheduler, set up so that the new task is not executed if the previous one is not completed.

PS


So, in just such a simple way, in a fairly short period of time, the unloading of the nomenclature / prices / balances to the site was implemented. Of course, this option has a number of drawbacks and does not claim the only true laurels, but at the same time we managed to fully fit into the restrictions set by the customer’s ecosystem. All this design works on weak machines, does not require a constantly running copy of 1C, works on the most ordinary virtual hosting, does not distract employees at all, and most importantly, allows 1C to serve unhindered. Underlay straw turned out to be enough, and for almost a year, fortunately, not one of our interventions in the operation of the system was required.

Source: https://habr.com/ru/post/139657/


All Articles