Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

Customs Dox Module (:mod:`tendril.dox.customs`) 

=============================================== 

 

This module generates the customs document sets. 

 

The functions here use the :mod:`tendril.dox.render` module to actually 

produce the output files after constructing the appropriate stage. 

 

.. hint:: The functions and templates here are made for and in the Indian 

          Customs formats. This probably doesn't translate well to any other 

          country. As such, this module itself doesn't do much of the heavy 

          lifting. Instead, it relies on the :mod:`tendril.sourcing.customs` 

          module. Porting it to another country would probably start from 

          reimplementing that class and rewriting the templates. 

 

.. seealso:: :mod:`tendril.dox`, :mod:`tendril.sourcing.customs` 

 

.. rubric:: Document Set Generators 

 

.. autosummary:: 

 

    generate_docs 

    gen_submitdocs 

    gen_verificationdocs 

 

.. rubric:: Document Generators 

 

.. autosummary:: 

 

    gen_declaration 

    gen_valuation 

    gen_rsp_declaration 

    gen_authorization 

    gen_tech_writeup 

    gen_verification_sections 

    gen_verification_checklist 

 

""" 

 

import os 

import datetime 

import copy 

import yaml 

import fs 

 

import render 

import wallet 

import docstore 

 

from tendril.utils import pdf 

from tendril.utils.db import with_db 

from tendril.entityhub import serialnos 

from tendril.utils.fsutils import get_tempname 

from tendril.utils.fsutils import temp_fs 

 

from tendril.utils.config import COMPANY_GOVT_POINT 

 

 

def gen_declaration(invoice, target_folder, copyt, serialno): 

    """ 

    Generates a copy of the Customs Declaration for Imports. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param copyt: A string specifying which copy it is ("ORIGINAL", 

                  "DUPLICATE", so on) 

    :type copyt: str 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file path 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/customs/decl.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/decl.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, from 

            :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data` 

        * - ``currency`` 

          - The symbol of the currency of the invoice. 

        * - ``inv_total`` 

          - The total value of the invoice, in vendor currency. 

        * - ``exchrate`` 

          - The applicable exchange rate. 

        * - ``exchnotif`` 

          - The government notification number specifying the exchange rate. 

        * - ``exchnotifdt`` 

          - The date of the exchange rate notification. 

        * - ``extended_total_sc`` 

          - The extended total invoice value, in the vendor's currency. 

        * - ``assessable_total_sc`` 

          - The assessable total invoice value, in the vendor's currency. 

        * - ``assessable_total_nc`` 

          - The assessable total invoice value, in the vendor's currency. 

        * - ``copyt`` 

          - The string specifying which copy it is. 

        * - ``sno`` 

          - The serial number of the document. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-declaration-" + copyt + '-' + str(invoice.inv_no) + ".pdf" 

    ) 

 

    given_data = copy.deepcopy(invoice.given_data) 

 

    for k, v in given_data['costs_not_included'].iteritems(): 

        given_data['costs_not_included'][k] = render.escape_latex(v) 

 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': given_data, 

             'currency': render.escape_latex(invoice.currency.symbol), 

             'inv_total': 

             render.escape_latex(invoice.extendedtotal.source_string), 

             'exchrate': invoice.given_data['exchrate'], 

             'exchnotif': invoice.given_data['exchnotif'], 

             'exchnotifdt': invoice.given_data['exchnotif_date'], 

             'extended_total_sc': 

             render.escape_latex(invoice.extendedtotal.source_string), 

             'assessable_total_sc': 

             render.escape_latex(invoice.assessabletotal.source_string), 

             'assessable_total_nc': 

             render.escape_latex(invoice.assessabletotal.native_string), 

             'copyt': copyt, 

             'sno': serialno + '.5' 

             } 

 

    return render.render_pdf(stage, 'customs/decl.tex', outpath) 

 

 

def gen_valuation(invoice, target_folder, serialno): 

    """ 

    Generates the Customs Valuation Note. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file path 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/customs/valuation.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/valuation.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, from 

            :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data` 

        * - ``currency`` 

          - The symbol of the currency of the invoice. 

        * - ``inv_total`` 

          - The total value of the invoice, in vendor currency. 

        * - ``exchrate`` 

          - The applicable exchange rate. 

        * - ``exchnotif`` 

          - The government notification number specifying the exchange rate. 

        * - ``exchnotifdt`` 

          - The date of the exchange rate notification. 

        * - ``note1`` 

          - A note mentioning the inclusion of freight. 

        * - ``note2`` 

          - A list of strings mentioning other additions to the valuation. 

        * - ``include_note2`` 

          - Boolean, whether or not to include note2. 

        * - ``extended_total_sc`` 

          - The extended total invoice value, in the vendor's currency. 

        * - ``assessable_total_sc`` 

          - The assessable total invoice value, in the vendor's currency. 

        * - ``assessable_total_nc`` 

          - The assessable total invoice value, in the vendor's currency. 

        * - ``copyt`` 

          - The string specifying which copy it is. 

        * - ``sno`` 

          - The serial number of the document. 

        * - ``is_wire`` 

          - Bool, whether the payment was made by a wire transfer. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-valuation-" + str(invoice.inv_no) + ".pdf" 

    ) 

 

    note1 = '' 

    if invoice.includes_freight is True: 

        note1 += "As listed in the invoice, {0} towards freight " \ 

                 "is also added. ".format(invoice.freight.source_string) 

    note1 = render.escape_latex(note1) 

 

    note2 = [] 

 

    if invoice.added_insurance is True: 

        note2.append( 

            "An additional {0}% of FOB is added to the assessable value " 

            "as per Rule 10(2)(c)(iii) of Customs Valuation " 

            "(Determination of Value of Imported Goods) Rules, 2007. " 

            "No specific insurance charges were paid as part of the" 

            " transaction.".format(invoice.insurance_pc) 

        ) 

    if invoice.added_handling is True: 

        note2.append( 

            "An additional {0}% of CIF is added to the assessable value " 

            "as per Rule 10(2)(b)(ii) of Customs Valuation " 

            "(Determination of Value of Imported Goods) Rules, 2007. " 

            "No specific handling charges were paid as part of the" 

            " transaction.".format(invoice.handling_pc) 

        ) 

 

    include_note2 = False 

    if len(note2) > 0: 

        include_note2 = True 

    if invoice.given_data['bank_ref'] is None: 

        is_wire = False 

    else: 

        is_wire = True 

 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'currency': render.escape_latex(invoice.currency.symbol), 

             'inv_total': 

             render.escape_latex(invoice.extendedtotal.source_string), 

             'exchrate': invoice.given_data['exchrate'], 

             'exchnotif': invoice.given_data['exchnotif'], 

             'exchnotifdt': invoice.given_data['exchnotif_date'], 

             'note1': note1, 

             'note2': note2, 

             'include_note2': include_note2, 

             'extended_total_sc': 

             render.escape_latex(invoice.extendedtotal.source_string), 

             'assessable_total_sc': 

             render.escape_latex(invoice.assessabletotal.source_string), 

             'assessable_total_nc': 

             render.escape_latex(invoice.assessabletotal.native_string), 

             'sno': serialno + '.3', 

             'is_wire': is_wire 

             } 

 

    return render.render_pdf(stage, 'customs/valuation.tex', outpath) 

 

 

def gen_rsp_declaration(invoice, target_folder, serialno): 

    """ 

    Generates the Customs Retail Sales Price Declaration. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file path 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/customs/rsp-declaration.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/rsp-declaration.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, 

            from :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data` 

        * - ``sno`` 

          - The serial number of the document. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-rsp-declaration-" + str(invoice.inv_no) + ".pdf" 

    ) 

 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'sno': serialno + '.2' 

             } 

 

    return render.render_pdf(stage, 'customs/rsp-declaration.tex', outpath) 

 

 

def gen_authorization(invoice, target_folder, serialno): 

    """ 

    Generates the Customs CHA Authorization Letter. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file path 

 

    .. rubric:: Template Used 

 

    This function uses a different template for each CHA, in the format 

    that the CHA asks for it. 

 

    Template Filename : 

    ``tendril/dox/templates/customs/authorization.<cha>.tex`` 

 

    Included Templates : 

 

    .. list-table:: 

 

        * - FedEx India 

          - ``tendril/dox/templates/customs/authorization.fedex.tex`` 

          - (:download:`Included version <../../tendril/dox/templates/\ 

customs/authorization.fedex.tex>`) 

        * - DHL India 

          - ``tendril/dox/templates/customs/authorization.dhl.tex`` 

          - (:download:`Included version <../../tendril/dox/templates/\ 

customs/authorization.dhl.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, 

            from :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data` 

        * - ``sno`` 

          - The serial number of the document. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-authorization-" + str(invoice.inv_no) + ".pdf" 

    ) 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'sno': serialno + '.1' 

             } 

 

    if invoice.given_data['cha'] == 'DHL': 

        template = 'customs/authorization.dhl.tex' 

    elif invoice.given_data['cha'] == 'FedEx': 

        template = 'customs/authorization.fedex.tex' 

    else: 

        raise ValueError('CHA unsupported') 

    return render.render_pdf(stage, template, outpath) 

 

 

def gen_tech_writeup(invoice, target_folder, serialno): 

    """ 

    Generates the Customs Technical Writeup. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file path 

 

    .. rubric:: Template Used 

 

    Template Filename : 

    ``tendril/dox/templates/customs/technical-writeup.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/technical-writeup.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, 

            from :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data` 

        * - ``sno`` 

          - The serial number of the document. 

        * - ``linecount`` 

          - The number of lines in the invoice. 

        * - ``tqty`` 

          - The total quantity. 

        * - ``tvalue`` 

          - The total value. 

        * - ``unclassified`` 

          - Lines in the invoice which could not be classified 

        * - ``sectable`` 

          - The secion table, containing a list of ``section lines``, 

            described below. The 'sections' here are Customs HS Codes. 

 

    .. list-table:: Section line keys 

 

        * - ``code`` 

          - The HS section code. 

        * - ``name`` 

          - The HS section name. 

        * - ``idxs`` 

          - Line numbers classified into this line. 

        * - ``qty`` 

          - Total quantity of all lines classified into this line. 

        * - ``value`` 

          - Total value of all lines classified into this line. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-tech-writeup-" + str(invoice.inv_no) + ".pdf" 

    ) 

    sectable = [] 

    tqty = 0 

    tvalue = 0 

    for section in invoice.hssections: 

        lval = invoice.getsection_assessabletotal(section).source_string 

        secline = {'code': section.code, 

                   'name': section.name, 

                   'idxs': invoice.getsection_idxs(section), 

                   'qty': invoice.getsection_qty(section), 

                   'value': render.escape_latex(lval) 

                   } 

        sectable.append(secline) 

        tqty += invoice.getsection_qty(section) 

        tvalue += invoice.getsection_assessabletotal(section) 

 

    unclassified = {'idxs': [x.idx for x in invoice.unclassified], 

                    'qty': sum([x.qty for x in invoice.unclassified]), 

                    } 

    if unclassified['qty'] > 0: 

        unclassified['value'] = \ 

            render.escape_latex( 

                sum([x.assessableprice 

                     for x in invoice.unclassified]).source_string) 

        tvalue += sum([x.assessableprice for x in invoice.unclassified]) 

        tqty += unclassified['qty'] 

    else: 

        unclassified['value'] = None 

 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'linecount': invoice.linecount, 

             'sectable': sectable, 

             'tqty': tqty, 

             'tvalue': render.escape_latex(tvalue.source_string), 

             'unclassified': unclassified, 

             'sno': serialno + '.4' 

             } 

    return render.render_pdf(stage, 'customs/technical-writeup.tex', outpath) 

 

 

def gen_submitdocs(invoice, target_folder, serialno): 

    """ 

    Generates collated PDF files containing the submittable 

    documents. Two PDF files are generated - one for printing on 

    the company letterhead and one on regular paper. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: List of output file tuples (path, type) 

 

 

    .. rubric:: Included Documents - For Letterhead Printing 

 

    * CHA Authorization, generated by :func:`gen_authorization` 

    * RSP Declaration, generated by :func:`gen_rsp_declaration` 

    * Note on Valuation, generated by :func:`gen_valuation` 

    * Technical Writeup, generated by :func:`gen_tech_writeup` 

 

    .. rubric:: Included Documents - For Plain Paper Printing 

 

    * Declaration (ORIGINAL), generated by :func:`gen_declaration` 

    * Declaration (DUPLICATE), generated by :func:`gen_declaration` 

    * Two copies of the Gatt Declaration, from :mod:`tendril.dox.wallet`, 

      key ``CUSTOMS-DECL``. 

    * Scanned copy of the IEC, from :mod:`tendril.dox.wallet`, 

      key ``IEC``. 

 

    """ 

    lh_files = [gen_authorization(invoice, target_folder, serialno), 

                gen_rsp_declaration(invoice, target_folder, serialno), 

                gen_valuation(invoice, target_folder, serialno), 

                gen_tech_writeup(invoice, target_folder, serialno) 

                ] 

    lh_fpath = os.path.join( 

        target_folder, "customs-printable-lh-" + str(invoice.inv_no) + ".pdf" 

    ) 

    lh_fpath = pdf.merge_pdf(lh_files, lh_fpath, remove_sources=True) 

 

    pp_files = [gen_declaration(invoice, target_folder, 

                                'ORIGINAL', serialno), 

                gen_declaration(invoice, target_folder, 

                                'DUPLICATE', serialno), 

                wallet.get_document_path('CUSTOMS-DECL'), 

                wallet.get_document_path('CUSTOMS-DECL'), 

                wallet.get_document_path('IEC'), 

                ] 

    pp_fpath = os.path.join( 

        target_folder, "customs-printable-pp-" + str(invoice.inv_no) + ".pdf" 

    ) 

    pp_fpath = pdf.merge_pdf(pp_files, pp_fpath, remove_sources=True) 

 

    files = [(lh_fpath, 'CUST-PRINTABLE-LH'), (pp_fpath, 'CUST-PRINTABLE-PP')] 

    return files 

 

 

def gen_verification_sections(invoice, target_folder, serialno): 

    """ 

    Generates the Customs Sections Verification document. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file tuple (path, type) 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/customs/verification-sections.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/verification-sections.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, 

            from :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data`. 

        * - ``lines`` 

          - A list of :class:`tendril.sourcing.customs.CustomsInvoiceLine` 

            instances. 

        * - ``sno`` 

          - The serial number of the document. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-verification-sections-" + str(invoice.inv_no) + ".pdf" 

    ) 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'lines': invoice.lines, 

             'sno': serialno + '.6', 

             } 

 

    outpath = render.render_pdf(stage, 

                                'customs/verification-sections.tex', 

                                outpath) 

    return outpath, 'CUST-VERIF-SEC' 

 

 

def gen_verification_checklist(invoice, target_folder, serialno): 

    """ 

    Generates the Customs Duties / Checklist Verification document. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: The output file tuple (path, type) 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/customs/verification-duties.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/customs/verification-duties.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``date`` 

          - The date the documents were generated at, 

            from :func:`datetime.date.today`. 

        * - ``signatory`` 

          - The name of the person who 'signs' the document, from 

            :data:`tendril.utils.config.COMPANY_GOVT_POINT`. 

        * - ``inv_no`` 

          - The vendor's invoice number. 

        * - ``inv_date`` 

          - The date of the vendor's invoice. 

        * - ``given_data`` 

          - A dict containing various facts about the invoice. See 

            :attr:`tendril.sourcing.customs.CustomsInvoice.given_data`. 

        * - ``lines`` 

          - A list of :class:`tendril.sourcing.customs.CustomsInvoiceLine` 

            instances. 

        * - ``invoice`` 

          - The :class:`tendril.sourcing.customs.CustomsInvoice` instance. 

        * - ``sno`` 

          - The serial number of the document. 

        * - ``summary`` 

          - A list of dicts containing the summary of the customs duties 

            applicable against a particular section, as described below 

 

    .. list-table:: Summary keys 

 

        * - ``section`` 

          - The HS section, a 

            :class:`tendril.sourcing.customs.CustomsSection`` instance. 

        * - ``code`` 

          - The HS section code. 

        * - ``name`` 

          - The HS section name. 

        * - ``idxs`` 

          - Line numbers classified into this line. 

        * - ``qty`` 

          - Total quantity of all lines classified into this line. 

        * - ``assessablevalue`` 

          - Total assessable value of all lines classified into this line. 

        * - ``bcd`` 

          - Total Basic Customs Duty applicable against this section. 

        * - ``cvd`` 

          - Total Countervailing Duty applicable against this section. 

        * - ``acvd`` 

          - Total Additional Countervailing Duty applicable against 

            this section. 

        * - ``cec`` 

          - Total Education Cess on Customs Duty applicable against 

            this section. 

        * - ``cshec`` 

          - Total Secondary and Higher Education Cess on Customs Duty 

            applicable against this section. 

        * - ``cvdec`` 

          - Total Education Cess on Countervailing Duty applicable 

            against this section. 

        * - ``cvdshec`` 

          - Total Secondary and Higher Education Cess on Countervailing Duty 

            applicable against this section. 

 

    """ 

    outpath = os.path.join( 

        target_folder, 

        "customs-verification-duties-" + str(invoice.inv_no) + ".pdf" 

    ) 

    summary = [] 

    for section in invoice.hssections: 

        secsum = {'section': section, 

                  'code': section.code, 

                  'name': section.name, 

                  'idxs': invoice.getsection_idxs(hssection=section), 

                  'assessablevalue': 

                  invoice.getsection_assessabletotal(hssection=section), 

                  'qty': invoice.getsection_qty(hssection=section), 

                  'bcd': 

                  sum([x.bcd.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'cvd': 

                  sum([x.cvd.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'acvd': 

                  sum([x.acvd.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'cec': 

                  sum([x.cec.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'cshec': 

                  sum([x.cshec.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'cvdec': 

                  sum([x.cvdec.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  'cvdshec': 

                  sum([x.cvdshec.value for x in 

                       invoice.getsection_lines(hssection=section)]), 

                  } 

        summary.append(secsum) 

    stage = {'date': datetime.date.today().isoformat(), 

             'signatory': COMPANY_GOVT_POINT, 

             'inv_no': invoice.inv_no, 

             'inv_date': invoice.inv_date, 

             'given_data': invoice.given_data, 

             'lines': invoice.lines, 

             'summary': summary, 

             'invoice': invoice, 

             'sno': serialno + '.7'} 

    outpath = render.render_pdf(stage, 

                                'customs/verification-duties.tex', 

                                outpath) 

    return outpath, 'CUST-VERIF-BOE' 

 

 

def gen_verificationdocs(invoice, target_folder, serialno): 

    """ 

    Generates customs verification documents. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to 

    :param serialno: The serial number of the Customs documentation set 

    :type serialno: str 

    :return: List of output file tuples (path, type) 

 

    .. rubric:: Included Documents 

 

    * Sections Verification, generated by :func:`gen_verification_sections` 

    * Duties / Checklist Verification, generated by 

      :func:`gen_verification_checklist` 

 

    """ 

    files = [gen_verification_sections(invoice, target_folder, serialno), 

             gen_verification_checklist(invoice, target_folder, serialno) 

             ] 

    return files 

 

 

def generate_docs(invoice, target_folder=None, 

                  serialno=None, register=False, efield=None): 

    """ 

    Generates all the Customs related documentation given a CustomsInvoice 

    (or subclass) instance. 

 

    :param invoice: The invoice object with customs information 

    :type invoice: :class:`tendril.sourcing.customs.CustomsInvoice` 

    :param target_folder: The folder in which the generated files 

                          should be written to. Auto sets to Instance scratch 

                          folder if None 

    :type target_folder: str 

    :param serialno: The serial number of the Customs documentation set. 

                     Autogenerates if None. 

    :type serialno: str 

    :param register: Whether or not to register in the docstore. 

                     Default False. 

    :type register: bool 

    :param efield: Additional short note to identify the document in the 

                   store. Autogenerates from invoice if None. 

    :type efield: str 

    :return: The serial number of the generated document set. 

 

    .. rubric:: Included Document Sets 

 

    * Sumbmittable Documents, generated by :func:`gen_submitdocs` 

    * Verification Documents, generated by :func:`gen_verificationdocs` 

 

    """ 

    if efield is None: 

        efield = ' '.join([invoice.vendor_name, str(invoice.inv_no)]) 

    if serialno is None: 

        if os.path.exists(os.path.join(invoice.source_folder, 'wsno')): 

            with open(os.path.join(invoice.source_folder, 'wsno'), 'r') as f: 

                serialno = f.readline() 

        else: 

            serialno = serialnos.get_serialno(series='PINV', 

                                              efield=efield, 

                                              register=register) 

    if target_folder is None: 

        target_folder = invoice.source_folder 

    files = gen_submitdocs(invoice, target_folder, serialno=serialno) 

    files.extend( 

        gen_verificationdocs(invoice, target_folder, serialno=serialno) 

    ) 

    files.extend( 

        invoice.source_files 

    ) 

    if register is True: 

        for document in files: 

            docstore.register_document(serialno=serialno, docpath=document[0], 

                                       doctype=document[1], 

                                       efield=efield, series='PINV') 

    return serialno 

 

 

@with_db 

def get_all_customs_invoice_serialnos(limit=None, session=None): 

    snos = docstore.controller.get_snos_by_document_doctype( 

        doctype='CUST-PRINTABLE-PP', series='PINV', 

        limit=limit, session=session 

    ) 

    return snos 

 

 

def get_customs_invoice(serialno): 

    documents = docstore.controller.get_sno_documents(serialno=serialno) 

    inv_yaml = None 

    for document in documents: 

        if document.doctype == 'INVOICE-DATA-YAML': 

            inv_yaml = document.docpath 

    if not inv_yaml: 

        raise ValueError('Invoice data not found for : ' + serialno) 

    with docstore.docstore_fs.open(inv_yaml, 'r') as f: 

        inv_data = yaml.load(f) 

 

    inv_format = inv_data['invoice_format'] 

 

    if inv_format == 'analogdevices': 

        from tendril.sourcing import pricelist 

        invoice_class = pricelist.AnalogDevicesInvoice 

    elif inv_format == 'digikey': 

        from tendril.sourcing import digikey 

        invoice_class = digikey.DigiKeyInvoice 

    else: 

        raise ValueError('Unrecognized Customs Invoice Format : ' + 

                         inv_format) 

 

    from tendril.sourcing import electronics 

    vobj = electronics.get_vendor_by_name(inv_format) 

 

    workspace_name = get_tempname() 

    docstore.copy_docs_to_workspace(serialno=serialno, 

                                    workspace=workspace_name, 

                                    clearws=True, 

                                    fs=temp_fs) 

 

    invoice = invoice_class( 

        vobj, 

        temp_fs.getsyspath( 

            fs.path.join(workspace_name, 'inv_data.yaml') 

        ) 

    ) 

 

    temp_fs.removedir(workspace_name, recursive=True, force=True) 

    return invoice